Possible Duplicate:
Trying to loop 3 threads in a specific order everytime
I want to access two different methods of the same object from two threads one after another. Here is my code,
public class ThreadCoordination
{
private Thread threadSayHello;
private Thread threadSayWorld;
private boolean threadSayWorldStarted = false;
public ThreadCoordination()
{
createThreads();
}
private void createThreads()
{
threadSayWorld = new Thread(new Runnable()
{
public void run()
{
try
{
// while (true)
{
sayWorld();
}
}
catch (InterruptedException ex)
{}
}
});
threadSayHello = new Thread(new Runnable()
{
public void run()
{
try
{
// while (true)
{
sayHello();
if (!threadSayWorldStarted)
{
threadSayWorldStarted = true;
threadSayWorld.start();
}
}
}
catch (InterruptedException ex)
{}
}
});
threadSayHello.start();
}
private synchronized void sayHello() throws InterruptedException
{
System.out.print("Hello ");
}
private synchronized void sayWorld() throws InterruptedException
{
System.out.println("World!");
}
public static void main(String[] args)
{
new ThreadCoordination();
}
}
If i uncomment the call while(true), then I’ll expecting the output like this,
Hello World!
Hello World!
Hello World!
Hello World!
...
Please guide me how do I do it.
Raja.
I don’t know whether I can edit the closed post. I just want to post the solution as far as I know.
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class SequenceAccess
{
private ReentrantLock myLock;
private Condition ensureSequence;
private int sequenceNo = 1;
public SequenceAccess()
{
myLock = new ReentrantLock();
ensureSequence = myLock.newCondition();
startThreads();
}
private void startThreads()
{
new Thread(new Runnable()
{
public void run()
{
try
{
while (true)
method1();
}
catch (InterruptedException ex)
{}
}
}).start();
new Thread(new Runnable()
{
public void run()
{
try
{
while (true)
method2();
}
catch (InterruptedException ex)
{}
}
}).start();
new Thread(new Runnable()
{
public void run()
{
try
{
while (true)
method3();
}
catch (InterruptedException ex)
{}
}
}).start();
}
private void method1() throws InterruptedException
{
myLock.lock();
try
{
while (sequenceNo != 1)
ensureSequence.await();
sequenceNo = 2;
System.out.println("Method 1");
ensureSequence.signalAll();
}
finally
{
myLock.unlock();
}
}
private void method2() throws InterruptedException
{
myLock.lock();
try
{
while (sequenceNo != 2)
ensureSequence.await();
sequenceNo = 3;
System.out.println("Method 2");
ensureSequence.signalAll();
}
finally
{
myLock.unlock();
}
}
private void method3() throws InterruptedException
{
myLock.lock();
try
{
while (sequenceNo != 3)
ensureSequence.await();
sequenceNo = 1;
System.out.println("Method 3");
ensureSequence.signalAll();
}
finally
{
myLock.unlock();
}
}
public static void main(String[] args)
{
new SequenceAccess();
}
}
The JVM does not guarantee order of thread execution
In fact the JVM spec could be met entirely by running the hello thread until it terminates and then running the world thread until it terminates (with your program as written)
You will need to introduce some sort of token to be shared between the two threads and then have that token shuttled back and forth.
The token could be as simple as a Boolean which is true if hello has been output and false if world has been output.
Each thread will then have to spin (or wait for a condition – better performance with a condition) until the Boolean matches its expected state.
I recommend getting your hands on the most excellent “java concurrency in practice” book