• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Thread's join()

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit and Somnath.

I got it
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Lookout! Runaway whale! Hide behind this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic