Java-JUC (10): Threads execute alternately Java: existing threads T1/T2/T3, how to ensure that T2 is executed after T1 is executed, and T3 is executed after T2 is executed.

question:

There are three threads a, b, and c, so that they are executed 10 times in sequence according to abc.

accomplish:

package com.dx.juc.test;

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

public class ABC {
    public static void main(String[] args) {
        final AlternateDemo alternateDemo=new AlternateDemo();
        
        new Thread(new Runnable() {
            public void run() {
                for(int i=1;i<10;i++){
                    alternateDemo.loopA (i);
                }
            }
        }, "A").start();
        
        new Thread(new Runnable() {
            public void run() {
                for(int i=1;i<10;i++){
                    alternateDemo.loopB(i);
                }
            }
        }, "B").start();
        
        new Thread(new Runnable() {
            public void run() {
                for(int i=1;i<10;i++){
                    alternateDemo.loopC(i);
                    System.out.println("---------------------------------------");
                }
            }
        }, "C").start();
    }
}

class AlternateDemo {
    private Lock lock = new ReentrantLock();

    private Condition conditionA = lock.newCondition();
    private Condition conditionB = lock.newCondition();
    private Condition conditionC = lock.newCondition();

    // A label that marks the thread number that can be executed currently. 1-a thread can be executed, 2-b thread can be executed, and 3-c thread can be executed. 
    private  int flag = 1 ;

    public  void loopA ( int loopNum)
        lock.lock();

        try {
             // 1) wait to wake up
             // if the flag value is not 1, let the thread wait until other threads wake it up. 
            if (flag != 1 ) {
                conditionA.await();
            }

            // 2) After being woken up, start executing. 
            for ( int i = 1; i <= 1; i++ ) {
                System.out.println(Thread.currentThread().getName() + "-" + loopNum + "-" + i);
            }

            // 3) Modify the flag flag and wake up the next thread. 
            flag = 2 ;
            conditionB.signal();
        } catch (InterruptedException e) {
            e.printStackTrace ();
        } finally {
            lock.unlock();
        }
    }

    public void loopB(int loopNum) {
        lock.lock();

        try {
             // 1) wait to wake up
             // if the flag value is not 2, let the thread wait until other threads wake it up. 
            if (flag != 2 ) {
                conditionB.await();
            }

            // 2) After being woken up, start executing. 
            for ( int i = 1; i <= 1; i++ ) {
                System.out.println(Thread.currentThread().getName() + "-" + loopNum + "-" + i);
            }

            // 3) Modify the flag flag and wake up the next thread. 
            flag = 3 ;
            conditionC.signal();
        } catch (InterruptedException e) {
            e.printStackTrace ();
        } finally {
            lock.unlock();
        }
    }

    public void loopC(int loopNum) {
        lock.lock();

        try {
             // 1) wait to wake up
             // if the flag value is not 3, let the thread wait until other threads wake it up. 
            if (flag != 3 ) {
                conditionC.await();
            }

            // 2) After being woken up, start executing. 
            for ( int i = 1; i <= 1; i++ ) {
                System.out.println(Thread.currentThread().getName() + "-" + loopNum + "-" + i);
            }

            // 3) Modify the flag flag and wake up the next thread. 
            flag = 1 ;
            conditionA.signal();
        } catch (InterruptedException e) {
            e.printStackTrace ();
        } finally {
            lock.unlock();
        }
    }
}

Test print result:

A-1-1
B-1-1
C-1-1
---------------------------------------
A-2-1
B-2-1
C-2-1
---------------------------------------
A-3-1
B-3-1
C-3-1
---------------------------------------
A-4-1
B-4-1
C-4-1
---------------------------------------
A-5-1
B-5-1
C-5-1
---------------------------------------
A-6-1
B-6-1
C-6-1
---------------------------------------
A-7-1
B-7-1
C-7-1
---------------------------------------
A-8-1
B-8-1
C-8-1
---------------------------------------
A-9-1
B-9-1
C-9-1
---------------------------------------

More implementation options:

Please refer to " Java: Existing threads T1/T2/T3, how to ensure that T2 is executed after the execution of T1 is completed, and T3 is executed after the execution of T2 is completed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325100458&siteId=291194637