Java 多条线程按照顺序执行

参考

代码

package thread;

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

/**
 * @Author 夏秋初
 * @Date 2022/2/28 09:36
 */
public class Thread3 {
    public static void main(String[] args) {
        Dog dog = new Dog();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    dog.c1();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    dog.c2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    dog.c3();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    dog.c4();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "D").start();

    }
}

class Dog {
    public Integer key = 1;

    Lock lock = new ReentrantLock();

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

    public void c1() throws InterruptedException {
        lock.lock();
        try {
            while (key != 1) {
                condition1.await();
            }
            key = 2;
            System.out.println(Thread.currentThread().getName());
            condition2.signal();
        } finally {
            lock.unlock();
        }
    }

    public void c2() throws InterruptedException {
        lock.lock();
        try {
            while (key != 2) {
                condition2.await();
            }
            key = 3;
            System.out.println(Thread.currentThread().getName());
            condition3.signal();
        } finally {
            lock.unlock();
        }
    }

    public void c3() throws InterruptedException {
        lock.lock();
        try {
            while (key != 3) {
                condition3.await();
            }
            key = 4;
            System.out.println(Thread.currentThread().getName());
            condition4.signal();
        } finally {
            lock.unlock();
        }
    }

    public void c4() throws InterruptedException {
        lock.lock();
        try {
            while (key != 4) {
                condition4.await();
            }
            key = 1;
            System.out.println(Thread.currentThread().getName());
            condition1.signal();
        } finally {
            lock.unlock();
        }
    }
}

运行结果

image

posted @ 2022-02-28 15:44  夏秋初  阅读(194)  评论(0)    收藏  举报