• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Explain Join() method of Thread

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Join() is non static method of Thread class per my understanding what it does is, it let one thread join at to the end of another Thread.
Lets see there are three Thread t1, t2,and t3. I want to say lets start t1 then t3 then t2. Is this right please explain the what I need to do bases on the senerio and my code below

Thread t1 = new Thread();
Thread t2 = new Thread();
Thread t3 = new Thread();

t1.start();
t2.start();
t3.start();

t1.join();
t3.join();
t2.join();
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call start on some thread, it will go to runnable state, then based on thread scheduler it may go to running state.

Your code is saying:

Start 3 threads, order in which they start executing run is not guaranteed and then wait until thread 1 is finished, thread 3 is finished, and thread 2 is finished, then proceed (with some code below).

If you want to be sure, that thread t1 enters run method as first, then you need to synchronize this action and wait (in your main thread) with starting other threads.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here join() causes the mainthread to wait until child threads execution completes.
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic