三个线程交替打印ABC10次

三个线程交替打印ABC,用到线程的通知机制:用一个共享变量来表示当前需要打印的线程

index 0打印A,然后将index修改为1,然后打印B,将index修改为2,打印C,然后将index修改为0

判断等待,业务处理(就是打印和修改index),唤醒等待

package cn.yishijie;

/**
 * 交替打印AB 10次
 */
public class ThreadNotify {

    public static void main(String[] args) {
         AB ab = new AB();

         new Thread(()->{
             for (int i =0;i<10;i++){
                 ab.printA();
             }
         }).start();

        new Thread(()->{
            for (int i =0;i<10;i++){
                ab.printB();
            }
        }).start();

        new Thread(()->{
            for (int i =0;i<10;i++){
                ab.printC();
            }
        }).start();
    }
}

class AB{
    private int index;

    public synchronized void printA(){
        // 这里一定用while,防止虚假唤醒
        while (index != 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print("A");
        index = 1;
        this.notifyAll();
    }

    public synchronized void printB(){
        while (index != 1){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.print("B");
        index = 2;
        this.notifyAll();
    }

    public synchronized void printC(){
        while (index != 2){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.print("C");
        index = 0;
        this.notifyAll();
    }
}

2、使用lock和condition精确唤醒

package cn.yishijie;

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

public class LockCondition {

    public static void main(String[] args) {
        ABC abc = new ABC();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                abc.printA();
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                abc.printB();
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                abc.printC();
            }
        }).start();
    }
}

class ABC {

    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();
    private int index = 1; // 1打印A,2打印B,3打印C

    public void printA() {
        lock.lock();
        try {
            while (index != 1) {
                condition1.await();
            }
            System.out.println("AAA");
            index = 2;
            condition2.signal();  // 精确唤醒
        } catch (Exception e) {

        } finally {
            lock.unlock();
        }
    }

    public void printB() {
        lock.lock();
        try {
            while (index != 2) {
                condition2.await();
            }

            System.out.println("BBB");
            index = 3;
            condition3.signal();
        } catch (Exception e) {

        } finally {
            lock.unlock();
        }
    }

    public void printC() {
        lock.lock();
        try {
            while (index != 3) {
                condition3.await();
            }

            System.out.println("CCC");
            index = 1;
            condition1.signal();
        } catch (Exception e) {

        } finally {
            lock.unlock();
        }
    }
}

 

你可能感兴趣的:(多线程)