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);
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.
Joined: Jul 29, 2011
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!!!
Joined: Sep 26, 2010
Try this code:
The output should be:
Is that what you were looking for?
subject: how to use join method so that thread1 should wait for thread2?