1つの面接問題:複数のスレッドが順番に出力されます.


テーマ:プログラムを作成して、3つのスレッドを开いて、この3つのスレッドのIDはそれぞれA、B、Cで、各スレッドは自分のIDをスクリーンの上で10回印刷して、出力の结果が必ず顺番に表示することを要求します.如:ABCABCABC…解題ソース:
package concurrent;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class AlternateDemo {
    //            ,1  A,2  B,3  C
    private int number = 1;

    Lock lock = new ReentrantLock();

    //Condition                     Condition
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();

    // loopNum:      
    public void loopA(int loopNum) {
        //  
        lock.lock();
        try {
            while (number != 1) {
                //  
                condition1.await();
            }

            System.out.println(Thread.currentThread().getName() + ", currentLoopNum is " + loopNum);
            number = 2;
            //  
            condition2.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //     
            lock.unlock();
        }
    }

    public void loopB(int loopNum) {
        lock.lock();
        try {
            while (number != 2) {
                condition2.await();
            }

            System.out.println(Thread.currentThread().getName() + ", currentLoopNum is " + loopNum);
            number = 3;
            condition3.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void loopC(int loopNum) {
        lock.lock();
        try {
            while (number != 3) {
                condition3.await();
            }

            System.out.println(Thread.currentThread().getName() + ", currentLoopNum is " + loopNum);
            number = 1;
            condition1.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

public class TestABCAlternate {

    public static void main(String[] args) {
        AlternateDemo ad = new AlternateDemo();

        new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++)
                    ad.loopA(i);
            }
        }, "A").start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++)
                    ad.loopB(i);
            }
        }, "B").start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++)
                    ad.loopC(i);
            }
        }, "C").start();
    }
}

この問題は、複数のスレッド間で異なるConditionを確立し、どのスレッドを起動するかを指定できるConditionの強みを十分に示しています.