Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use join method so that thread1 should wait for thread2?

 
Abhineet Joon
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.thread;

public class RunnableClass implements Runnable
{

public void run()
{
go();
}
public void go()
{
try
{
Thread.sleep(2000);
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
doMore();
}
public void doMore()
{
System.out.println("");
for(int i=0;i<10;i++)
{
System.out.println(Thread.currentThread().getName()+" is executing");
}

}

}
------------------------------------------------------------------

package com.thread;

public class ThreadClass
{
public static void main(String[] args)
{
System.out.println("Inside Main Method");
RunnableClass runnableClass = new RunnableClass();
//RunnableSecondClass runnableSecondClass = new RunnableSecondClass();
Thread threadA = new Thread(runnableClass);
Thread threadB = new Thread(runnableClass);

threadA.setName("Thread A");
threadB.setName("Thread B");
threadA.start();
threadB.start();

try
{
threadA.join();

}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" is executing......");
}

}

If I want that thread B should wait for thread A to complete. Then where in the code shall I use the join method and how ?
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new at this, so take these recommendations with a grain of salt.

First of all, I believe that join means to join the referenced thread up with the current thread. The way you have it, with threadA.join(); on the main thread means that the main thread should wait for the threadA to complete.

I'm not clear on what you want to do, but here is one approach.

In the RunnableClass add a field of type Runnable and set it to null, call it threadToWaitFor.
Add this member to RunnableClass:



At the end of your run function, add the lines



The above will cause the thread running to wait for thread in field threadToWaitFor to complete.
Since you want threadB to wait for threadA to complete, then, in main, add the statement



I would add print statements at the start/end of run, and before/after the join statement, to indicate the process is started/completed or waiting/done-waiting on other thread.

Good Luck,
John
 
Abhineet Joon
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply but the snippet this.threadToWaitFor.join() is not working properly. I believe there is some other way round to solve this issue!!!
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code:




The output should be:



Is that what you were looking for?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic