wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread,Join problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread,Join problem" Watch "Thread,Join problem" New topic
Author

Thread,Join problem

thejaka samarakoon
Ranch Hand

Joined: Jul 20, 2010
Posts: 56



In this code the output is

New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
One: 5
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Three: 5
l.................................................more output


Why after out put of "New thread: Thread[One,5,main]" not be the out put "One:5" .can anybody explain why?"Is all 3 threads constructors call before run() method.
thejaka samarakoon
Ranch Hand

Joined: Jul 20, 2010
Posts: 56
can anyone help me on this.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi,

The class should be public, not ublic

thejaka samarakoon wrote:Is all 3 threads constructors call before run() method

Well, when constructor for ob1 gets executed, it creates and starts a thread. Now, since, it is a separate thread, there is no guarantee that it will finish its execution before next step of main thread.

Here, what is happening is : constructor of ob1 prints its name and creates and starts thread of that name. Same happens for other objects of Jreq, but this behaviour is not guaranteed. Rather, there is no guarantee that just because thread1 is started before thread2, hence run method of thread1 will start execution before run method of thread2.

I hope you get the point.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
There are actually 4 threads main, One, Two Three involved.
When the Jreq ob1, ob2, ob3 are instantiated, the One, Two ,Three threads start.
Therefore the 4 threads take turns to run. There is no guarantee which one runs first.

I got a question about the three join() method calls.

ob1.t.join() means letting ob1.t finishes first before the other threads.
So, it should be let ob1 print 5, 4, 3, 2,1 first, other thread executes.
I expect the output may look like this:
One: 5
One : 4
One:3
One:2
One:1
Two :5
Two :4
Two : 3
Two :2
Two:1
But the output is not.
The output is
One : 5
Two :5
Three: 5
One : 4
Two 4:
Three: 4

I have never learned multiple join method call. I think the first thread to call join(), should finish first , the second thread to call join() should finish next ...
But it seem to me that the threads takes turns to run before the main thread.
Can you explain why?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, I tried to comment out the Thread.sleep() catch (InterruptedException e){ ....} in the run method, so that the threads don't sleep.
I got the output like:
One : 5
One :4
One :3
One : 2
One :1
One exist;
Three :5
...
Two:5
....

I think when One sleeps, Two or Three can execute and so on.
When the sleep statement are not included, one thread will join the main thread , finishes and the second thread will join and so on.... the main thread will wait until all the threads finish.

By the way, one more observation. ob1.t.join is called first, but it does not mean it can join first.
In the code ob1.t.join(); ob2.t.join(); ob3.t.join(). It does not mean One joins first, then Two and then Three. There is no guarantee in this order.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
By the way, to answer the original question,
the three threads are started by the main thread. But it does not mean they call the run() immediately. When they are started, they are in the runnable state until the CPU give them the chance to run and then they enter the running state and invoke the run() method.

Correct me if I am wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread,Join problem