File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

join()??

 
srikanth reddy
Ranch Hand
Posts: 252
  • 0
  • Mark post as helpful
  • send pies
  • 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??
 
M Jairam
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • 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 + " ");

}
}
};
}}
 
Kevin Lam
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about if main() just call join() but not t.join()? what does that mean?
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic