• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unexpected result with Threads join() method...

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In theory I understand the way Threads join() method works, but when I am testing it with some code, I am getting unexpected results. I thought if you say:
1. start a thread1
2. start a thread2
3 do thread2.join()
I thought the result would be thread1 will pause until thread2 finishes then go back to runnable and eventually finish. Here's my code with an exlanation of results:

No matter how I change the for loop counter int in run(), Fred always finishes before Lucy. Surely he has joined the end of Lucy and must wait till she finishes.
Thanx for any help
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you have stated about the join() method is exactly right.
The analysis of your code can be done as follows:
There are 3 threads running namely,
1>main thread
2>Fred thread
3>Lucy thread
The code starts the threads "Fred" and "Lucy" in the "main" thread.
Therefore,we call the join() method on the "main" thread and not on the thread named "Fred".Hence there is nothing you can do with the run() method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic