aspose file tools*
The moose likes Threads and Synchronization and the fly likes how to use join method so that thread1 should wait for thread2? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "how to use join method so that thread1 should wait for thread2?" Watch "how to use join method so that thread1 should wait for thread2?" New topic
Author

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

Abhineet Joon
Greenhorn

Joined: Jul 29, 2011
Posts: 17
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

Joined: Sep 26, 2010
Posts: 139
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

Joined: Jul 29, 2011
Posts: 17
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

Joined: Sep 26, 2010
Posts: 139
Try this code:




The output should be:



Is that what you were looking for?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use join method so that thread1 should wait for thread2?
 
Similar Threads
Lock Mechanism Doubt
wait and notifyAll
Thread join method question?
After notify()
Threads and 1.4 exam