This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A Simple Problem with join()...

 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends, i have written a simple program...





The output is...

One : 0..<>One : 1..<>One : 2..<>One : 3..<>One : 4..<>
Two : 0..<>Two : 1..<>Two : 2..<>Two : 3..<>Two : 4..<>
Three : 0..<>Three : 1..<>Three : 2..<>Three : 3..<>Three : 4..<>

Press any key to continue...




My problem is i dont understand who is joining whom,
i actually dont know how join() works,
i have read the book, but it still is not very clear to me...

Regards
Prashanth Lingala
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The join() method, when called on thread "t" for instance, makes the currently executing thread (main in your case) wait until thread "t" dies (the run() method returns).
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Prashanth,

t.join() may be read as : "stop the execution at this point until t has finished". In your sample code, the JVM creates a thread (named "main thread") in which your application runs then main()creates and starts 3 other threads. The join() methods simply force the main thread to stop until t1 and t2 has died (at the end of the run() method).

Hope that helps,
 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James and Lionel
Thank you

Regards
Prashanth Lingala
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our "Threads and Synchronization" forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic