啓動3個線程輪番打印遞增的數字

啓動3個線程打印遞增的數字, 線程1先打印1,2,3,4,5, 而後是線程2打印6,7,8,9,10, 而後是線程3打印11,12,13,14,15. 接着再由線程1打印16,17,18,19,20....以此類推, 直到打印到75. 程序的輸出結果應該爲:java

線程1: 1c++

線程1: 2ide

線程1: 3this

線程1: 4線程

線程1: 5code

 

線程2: 6orm

線程2: 7get

線程2: 8it

線程2: 9io

線程2: 10

...

 

線程3: 71

線程3: 72

線程3: 73

線程3: 74

線程3: 75

public class PrintNoDemo {
    private static int printNO = 1;
    private static int printThread = 1;
    private static Condition condition1;
    private static Condition condition2;
    private static Condition condition3;

    private static ReentrantLock lock = new ReentrantLock();
    
    public static void main(String[] args){
        condition1 = lock.newCondition();
        condition2 = lock.newCondition();
        condition3 = lock.newCondition();
        
        for(int i=0; i<3; i++){
            Thread thread = new Thread(new Runnable(){

                @Override
                public void run() {
                    while(printNO <= 75){
                        printNo();
                    }
                }
            },"線程"+(i+1));
            thread.start();
        }
    }

    public static void printNo() {
        try {
            lock.lock();
            String threadName = Thread.currentThread().getName();
            //判斷線程,選擇等待Condition
            if(!threadName.equals("線程"+printThread)){
                if(threadName.equals("線程1")){
                    condition1.await();
                } else 
                if(threadName.equals("線程2")){
                    condition2.await();
                } else
                if(threadName.equals("線程3")){
                    condition3.await();
                }
            }
            
            //處理打印
            for (int i = 0; i < 5; i++) {
                if(printNO > 75){
                    break;
                }
                System.out.println(Thread.currentThread().getName() +":"+ printNO++);
            }
            //獲取下一個打印線程
            printThread = printThread%3 +1;
            
            if(threadName.equals("線程1")){
                condition2.signal();
            }else 
            if(threadName.equals("線程2")){
                condition3.signal();
            }else 
            if(threadName.equals("線程3")){
                condition1.signal();
            }
            
//            lock.notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}

網友實現方法

class NumberPrinter extends Thread {
    static int c = 0;
    static int state = 0;
    private int id;

    @Override
    public synchronized void run() {
        while (state < 15) {
            if (state % 3 == id) {
                for (int j = 0; j < 5; j++) {
                    c++;
                    System.out.format("Thread %d: %d %n", id, c);
                }
                state++;; 
            }
        }
    }

    public NumberPrinter(int id) {
        this.id = id;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new NumberPrinter(i).start();
        }
    }
}
相關文章
相關標籤/搜索