• Post Reply Bookmark Topic Watch Topic
  • New Topic

can i call two threads at the same time  RSS feed

 
prabhu das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Iam connecting to a socket and getting a input stream and output stream and written two threads.
One thread will read from the socket and other will write to the socket.
My main program is like this :
public static void main(String ar[]) {

Socket socket = getConnection();//this method will return me a socket
object
readThread().start();
writeThread().start();
}
when i am starting readThread() the read thread is continuously fetching the data from the socket and not ending since it is looking for more data.
Actually i have to start my writeTheard simultaneosly with read thread but while readThread is not ending my program is never going to write thread.

Can any please explain me how to call two threads simultaneously

Thanks in Advance..

Prabhu
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you showed should work.

How do you know your writeThread isn't started?
 
prabhu das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it is not working because, i have written some log's and if it send some outgoing commands the server socket would have sent me some message received command..
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your example main program shows a socket being opened, then ignored by the two threads (since it's stored in a local variable,) so it's obviously nothing like your real program. We can't tell you what's wrong with your code unless you show us actual code! Have a look through the "How To Ask Questions" link in my signature, and then come back and try to give us some more relevant details.
 
prabhu das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my sample code :

My main method :

class Main {

public static void main(String ar[]) {

try {

Socket socket = new Socket("url",portNumber);

MessageReader readThread = new MessageReader(socket);
readThread.start();

MessageWriter writeThread = new MessageWriter(socket);
writeThread.start();

}catch(IOException ie){
ie.printStackTrace();
}
}
}

My Read Thread is :

//As Socket is not sending me end of stream iam finding the end of stream by the message length in command sent by the socket.

public class MessageReader extends Thread
{
private static int eof = -1;

private static String string;

private static int messageLength;

private static int byteRec = 0;

public Socket socket = null;

public MessageReader(Socket socket) {
this.socket = socket;
}

/**
* @param socket
* This Method is to read From the Input Stream and give the STRING to the Session Bean
*/

public static void readMessage(Socket socket)
{
log.info(" In Read Message ");
try
{
log.info(" going to read the inputstream ");

StringBuffer buffer = new StringBuffer();
StringBuffer buffer2 = new StringBuffer();
int in;

while(true)
{
in = socket.getInputStream().read();

byteRec++;

String incomingString = Integer.toHexString(in);

if(incomingString.length() < 2)
{
incomingString ="0"+incomingString;
}//end of if
buffer.append(incomingString);
string = buffer.toString();

if(byteRec == 12)
{
messageLength = //Calling the parsing function here

log.info(" Length of the Message excluding headers is " + messageLength +" Bytes ");
eof = 12 + messageLength + 1;
log.info(" Length of the Message including the headers and the checksum is "+ eof +" Bytes ");
}//end of if

if(byteRec == eof)
{
buffer = buffer2;
byteRec = 0;
log.info(" Size of the string is "+string.length());

if(13+messageLength == (string.length()/2)) {
// as we got one command complete iam sending to other class which will presist this command
}
}// end of while
}//end of try
catch (IOException e)
{
log.info(" Error reading from InputStream "+e);//
}//end of catch
}//end of else
}//end of readMessage method

public void run() {
readMessage(socket);
}//end of run method

}// end of class


// MY Write Thread is

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class MessageWriter extends Thread
{
public Socket socket = null;

public MessageWriter(Socket socket)
{
this.socket = socket;
}
public void writeMessage(Socket socket) {

try {
OutputStream os = socket.getOutputStream();
byte[] bytes = {3,0,0,1,2,3,0,0,1,1,0,9,118,111,108,118,111,53,49,53,0,26};
os.write(bytes);

}catch(IOException ie) {
ie.printStackTrace();
}
}
public void run(){
writeMessage(socket);
}
}
[ July 27, 2006: Message edited by: prabhu das ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You send a very small number of bytes (22?), but never flush or close the OutputStream. It is extremely likely that the operating system is buffering those bytes, waiting for some more data before it bothers to send them over the network. Closing the OutputStream when you are through with it will force the data to be sent; this should fix your problem.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prabhu das:
I know it is not working because, i have written some log's and if it send some outgoing commands the server socket would have sent me some message received command..


The way to know whether your writing thread gets started is to put some logging at the beginning of the MessageWriters run method and observe whether you get the output.

As Ernest suggested, it's likely that the thread actually starts, and the problem is actually encountered later in the thread.
 
prabhu das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added os.close(); and one system.out.println("");

public class MessageWriter extends Thread
{
public OutputStream os;
public void write(Socket socket){

try {
System.out.println(" In Write Message ");
os = socket.getOutputStream();
byte[] bytes = {3,0,0,1,2,3,0,0,1,1,0,9,118,111,108,118,111,53,49,53,0,26};
os.write(bytes);

os.close();
}catch(IOException ie) {
ie.printStackTrace();
}
}
}
And the result is the same,it is not even printing "In Write Message".
My doubt is,as the read message function is continuosly looking for the data in the socket it seems to be blocking the whole process.

Can i call both the process at the same time ?
Please help me in this

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prabhu das:

Can i call both the process at the same time ?
Please help me in this


What sort of JVM is this running on? Maybe you are running in an environment where the read thread can preempt all other threads.
 
prabhu das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we are using java version=1.4.2_07 under eclipse environment
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with the writer, but I've noticed a significant number of other problems with the reader and other classes: the MessageReader "constructor" is declared with a "void" return type, making it not a constuctor at all, but an ordinary function which unfortunately shares the name of the class. This means, among other things, that the main() you've shown won't compile against this main().

The main() won't compile anyway, as there's no exception handling. Also, all of these classes are using an undeclared "log" object.

The first step to solving a problem like this is isolating it and making it repeatable. You still obviously haven't done that; you're still showing bits and pieces plucked from a larger program. Try to create the smallest possible program that shows the problem: in the process, you may discover the problem yourself, but if not, it will still be easier for others to help you.
 
prabhu das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have edited the above program , the real program is also the same as the above and but in real write message iam taking values from the dto's and sending for that reason i have given some bytes to make sure that it looks like the real program.
the bytes which iam showing is actually a acknowledgement to one of the command sent by the server socket.

My question is can i call two threads from one programm simutaneously,because in my above program first the main method will call the read class and then the write class, here iam facing problem as the read writer is not ending (and it should not because it should always ask for the data in the server).
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. If you create two Thread objects and call start() on them both, then your original thread, as well as the two you created, will all be running simultaneously.
 
prabhu das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for clearing my doubt
Can you please provide me some examples to start two threads simultaneously please.
Iam unable to find the examples anywhere

Thanks In Advance
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prabhu das:
Thank you very much for clearing my doubt
Can you please provide me some examples to start two threads simultaneously please.
Iam unable to find the examples anywhere

Thanks In Advance


Prabhu, what we are trying to tell you from the beginning is that your code *is* an example that does exactly that. The Thread.start method *does* return immediately. It does *not* wait for the thread to end.

At least not in a VM that works like it is supposed to work.
 
prabhu das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja,

My program really is not the example of what i require,
In my main method first iam calling read thread and then write thread, i dont want this. i want both to be started at the same time.

For example: i have a main method and two threads say A and B .
If time right now is 10 am and i should call both the threads at 10 am.
I dont want it to be delayed for even one sec.

I think i have explained in a correct way...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhu, THAT'S WHAT YOUR CODE DOES. Or rather, that's what your code looks like it would do, if your code were actually compilable. If you disagree, PLEASE PROVIDE COMPILABLE CODE so that people here can discuss something more concrete. Otherwise the best we can do is guess right now, and our best guesses are that your code would do what you wanted, if it were fixed so that it could compile.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prabhu das:
For example: i have a main method and two threads say A and B .
If time right now is 10 am and i should call both the threads at 10 am.
I dont want it to be delayed for even one sec.


The way you have done it, they should start at the same time, minus perhaps a couple of nanoseconds. For sure. Really. Really, really, really. Seriously.
 
prabhu das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that's what iam trying to say it will take some time to start second thread.

so it means that read thread is starting first but i dont want to do that i want to call them at the same time (not even nano seconds delay)

can i do it?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a multiprocessor machine, I suppose there's always a chance that they'd start at exactly the same clock-tick. But on a single processor, of course not; the CPU can only execute one instruction at a time, which of course means that one thread must start before the other.

Jim and Ilja and I are all telling you that what you're asking for doesn't matter, anyway. The problem you're seeing has nothing to do with threads starting at the same time, or not. The problem is something else, and we'd all love to help you figure it out, but you've been refusing to do the one simple thing we ask: present a complete program that compiles and runs and shows the problem. Not a fragment, but the exact, complete code to an actual full program you have run which shows the problem you're having. Every single line of code.

Until you do that, there is nothing more anyone can do to help.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prabhu das:
so it means that read thread is starting first but i dont want to do that i want to call them at the same time (not even nano seconds delay)


Why?
 
prabhu das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

Today i have copied the same file in my friends machine(which has same configuration as mine and same operating system i.e windows xp)and that code is working fine..
I feel that there should some problem with eclipse, what do you say?
If this is the configuration issue how we can solve this problem ?

Thanks
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!