Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

join()??

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


fnrds.

what my doubt is here calling join means that the main thread will have to wait until the Thread T1 has finished which is not started..so i thought that there should be no Output..but it gives End..will it not wait forever??
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Note that t1.join(); says that put the current thread on hold until thread t1 has finished.
2. In your example, t1 thread does not even exist - thread only exist after a call to its start() method.

To see join() in action, I have just made slight change to your program. Try it with and without the t1.join();. You will then see how join() works.

Hope this helps.

class TechnoSample {
public static void main(String[] args) throws Exception{
Thread t1 = new Thread(getRunnable(20, "t1"));
Thread t2 = new Thread(getRunnable(10, "t2"));
t1.start();
//t1.join();
t2.start();

System.out.println("End");
}
public static Runnable getRunnable(final int id, final String name){
return new Runnable(){
public void run(){
for(int i = 0; i < id; i++){
System.out.print(name+i + " ");

}
}
};
}}
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about if main() just call join() but not t.join()? what does that mean?
 
Sheriff
Posts: 22579
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no (static) join() method so that won't even compile. You can only join on a thread object (well, also on DatagramSocketImpl, but that's not what is meant here ).
 
Kevin Lam
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see

But what about within the Runnable object, it calls join() without referencing to other threads?

Kev
 
Rob Spoor
Sheriff
Posts: 22579
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where does it do that?? It could only do that if the Runnable is also a Thread.

If you try to join on the same thread (like Thread.currentThread.join()) you would get deadlock - it is waiting for itself to end, which it will never do because it is waiting.
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no black magic happening here. All the join() method does is call the isAlive() method. And if it is alive, it will just call the wait() method. This repeats until the thread is no longer alive.

And BTW, notification is sent by the cleanup code for the thread.

Henry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic