• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Thread,Join problem

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone help me on this.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic