aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Thread's join() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Thread Watch "Regarding Thread New topic
Author

Regarding Thread's join()

Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
class Thread1 implements Runnable
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("In Thread1 "+Thread.currentThread().getName() +" Is Executing");
}
}
}



public class ThreadJoin
{
public static void main(String [] arg)
{
Thread1 t1=new Thread1();


Thread th1=new Thread(t1);
th1.setName("Lucy");
Thread th2=new Thread(t1);
th2.setName("Fred");
th1.start();
th2.start();
try
{
th2.join();
}
catch(InterruptedException e)
{
System.out.println(e);
}




}
}



in the above mentioned code join() is not performing as it should.

when Fred encounters ,it should halt the current thread execution until it

finishes it's own.

it's giving following output

C:\JAVADeepak\L9>java ThreadJoin
In Thread1 Lucy Is Executing
In Thread1 Lucy Is Executing
In Thread1 Lucy Is Executing
In Thread1 Lucy Is Executing
In Thread1 Lucy Is Executing
In Thread1 Lucy Is Executing
In Thread1 Lucy Is Executing
In Thread1 Lucy Is Executing
In Thread1 Fred Is Executing ********Fred Enters
In Thread1 Fred Is Executing
In Thread1 Fred Is Executing
In Thread1 Fred Is Executing
In Thread1 Fred Is Executing
In Thread1 Fred Is Executing
In Thread1 Fred Is Executing
In Thread1 Fred Is Executing
In Thread1 Lucy Is Executing *********
In Thread1 Lucy Is Executing
In Thread1 Fred Is Executing *********
In Thread1 Fred Is Executing



Is there something wrong with code?


Thank You

Deepak Bahubal


Constant dripping hollows out a stone....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

The program is behaving as it should. It will stop the main thread till thread2 completes. thread1 is free to execute. If you add a S.O.P. as the last line of main method, then that line will only be displayed after thread2 will complete execution...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Hi,
When you say: th2.join(), it means that the current thread joins with th2. Now since this line of code is in the main method, so the main thread blocks till thread-th2 completes.
And till the main thread blocks, both threads th1 and th2 are free to execute in whatever order JVM wishes them to.
So th2.join() doesn't mean that th2 blocks, it means that the current executing thread blocks till th2 executes.
Hope you get this.


Preparing for SCJP 6.0
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
Thanks Ankit and Somnath.

I got it
Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61
Dear Ankit,

as per you said above

It will stop the main thread till thread2 completes


I still didn't get it. what is main thread?? is it main method?? and from the above code when I know that main thread has stopped??
really appreciate for your kind explanation.

regards,
Vierda Mila


SCJP 5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

every program that you run has a main thread. It is a thread created by the JVM in which it calls the main method. So when you start a program, JVM creates a main thread, and calls the main method in that thread.

Every statement in a program is executed in the main thread except if you create a thread by yourself and call start on it. So in normal execution, every call to sleep, join or wait will stop or suspend the main thread. If you create a new thread and call start on it and call sleep or join or wait in the run method of that thread, then that thread will be suspended and not the main thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Thread's join()