原创

线程通信之多个线程循环执行,(A线程执行5次,B线程执行10次,C线程执行15次)

Lock + Condition的方式

这里有三个condition
condition1执行完了,就去通知condition2.signal()

condition2执行完了,就去通知condition3.signal()

condition3执行完了,就去通知condition1.signal()

示例代码:

package com.debuggg.test1.main2;

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

public class TestAlternateDemo{
    public static void main(String[] args) {
        AlternateDemo alternateDemo = new AlternateDemo();
        Thread threadA = new Thread(new ThreadA(alternateDemo));
        Thread threadB = new Thread(new ThreadB(alternateDemo));
        Thread threadC = new Thread(new ThreadC(alternateDemo));
        threadA.start();
        threadB.start();
        threadC.start();
    }
}
//交替打印ABC,A 5次,B 10次,C 15次
class AlternateDemo {
    private int number = 1;//当前正在执行的线程

    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

    public void loopA(int loopTime){
        //1.判断当前控制执行的线程是否是number = 1;
        lock.lock();
        try {
            if (number != 1){
                try {
                    condition1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < 5; i++) {
                System.out.println("A" + (i + 1) + " loopTime: " + loopTime);
            }
            number = 2;
            condition2.signal();
        } finally {
            lock.unlock();
        }
    }
    public void loopB(int loopTime){
        //1.判断当前控制执行的线程是否是number = 2;
        lock.lock();
        try {
            if (number != 2){
                try {
                    condition2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < 10; i++) {
                System.out.println("B" + (i + 1) + " loopTime: " + loopTime);
            }
            number = 3;
            condition3.signal();
        } finally {
            lock.unlock();
        }

    }
    public void loopC(int loopTime){
        //1.判断当前控制执行的线程是否是number = 3;
        lock.lock();
        try {
            if (number != 3){
                try {
                    condition3.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < 15; i++) {
                System.out.println("C" + (i + 1) + " loopTime: " + loopTime);
            }
            number = 1;
            condition1.signal();
        } finally {
            lock.unlock();
        }
    }

}
class ThreadA  implements Runnable{

    private AlternateDemo alternateDemo;

    public ThreadA(AlternateDemo alternateDemo){
        this.alternateDemo = alternateDemo;
    }

    @Override
    public void run() {
        int number = 1;
        while (true) {
            alternateDemo.loopA( number ++);
        }
    }
}

class ThreadB  implements Runnable{

    private AlternateDemo alternateDemo;

    public ThreadB(AlternateDemo alternateDemo){
        this.alternateDemo = alternateDemo;
    }

    @Override
    public void run() {
        int number = 1;
        while (true) {
            alternateDemo.loopB(number ++ );
        }
    }
}
class ThreadC  implements Runnable{

    private AlternateDemo alternateDemo;

    public ThreadC(AlternateDemo alternateDemo){
        this.alternateDemo = alternateDemo;
    }

    @Override
    public void run() {
        int number = 1;
        while (true) {
            alternateDemo.loopC(number ++);
        }
    }
}
正文到此结束
本文目录