java 多线程(生产者消费者)

时间:2022-10-26 13:06:21

转 https://www.oschina.net/code/snippet_111708_25438

这个问题挺经典,我这个解法的本质在于将问题抽象为生产者消费者模型,但是是一个特殊的生产者消费者模型,有两点要求:
1、缓冲区大小为1(用一个布尔变量表示就可以了)
2、缓冲区初始为空
再具体点可以将其想象为一个一次只能放一张纸打印的打印机,放纸的线程是A,打印的线程是B。初始状态打印机没有纸。

// 打印机类
public class Printer {
     
    private boolean hasBufferToPrint = false;   // 打印缓冲区是否有内容可以打印
 
    // 打印A:相当于生产者,放一张纸
    public synchronized void printA() {
        while(hasBufferToPrint) {   // 缓冲区还有内容
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
         
        System.out.print("A");
        hasBufferToPrint = true;
         
        notify();   // 唤醒打印B的线程
    }
     
    // 打印B:相当于消费者,消耗缓冲区中的纸,打印纸张
    public synchronized void printB() {
        while (!hasBufferToPrint) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
         
        System.out.print("B");
        hasBufferToPrint = false;
         
        notify();   // 唤醒打印A的线程
    }
 
    static class ThreadA extends Thread {
        private Printer printer;
 
        public ThreadA(Printer printer) {
            this.printer = printer;
        }
 
        public void run() {
            for(int i = 0; i < 10; i++) {
                printer.printA();
            }
        }
    }
 
    static class ThreadB extends Thread {
        private Printer printer;
         
        public ThreadB(Printer printer) {
            this.printer = printer;
        }
         
        public void run() {
            for(int i = 0; i < 10; i++) {
                printer.printB();
            }
        }
    }
 
    public static void main(String args[]) {
        Printer printer = new Printer();   // A、B线程共享同一个打印机
        Thread a = new ThreadA(printer);
        Thread b = new ThreadB(printer);
         
        a.start();
        b.start();
    }
}