可能重复:
每次都尝试以特定顺序循环 3 个线程
我想一个接一个地从两个线程访问同一对象的两个不同方法。这是我的代码,
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();
}
}
如果我取消注释调用 while(true),那么我会期待这样的输出,
Hello World!
Hello World!
Hello World!
Hello World!
...
请指导我该怎么做。拉贾。
我不知道我是否可以编辑已关闭的帖子。据我所知,我只想发布解决方案。
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();
}
}