本文共 2567 字,大约阅读时间需要 8 分钟。
多线程的使用往往不单单是单个线程的实现,而是多个线程的相互协作实现逻辑业务。
什么是线程通讯?
多个线程协作完成共享资源的使用,实现线程安全,线程不死锁。线程通讯实现方式:等待(wait()) | 通知 notify() .notifyall()
wait()、notify()、notifyall() 方法都是Object类下的方法。wait() : 让当前线程进入等到状态,并且释放锁资源。
notify() : 唤醒一个等待线程,且随机线程。 notifyall() : 唤醒所有等待线程。生产者消费者是一种现场和场景,并不是一种模式。管道法: 类似于缓存机制,生产者将生产的食物放置容器中,消费者从容器中取食物。
案例:
生产者: 生产食物。 消费者:消费食物。 食物容器:食物不足10个的时候需要生产者生产,且放入食物到容器中,消费者从容器中取食物,没有食物时,只能等待生产者生产食物。 实现:// 食物实体类 static class Food{ }
// 食物容器类(公共资源)static class FoodContainer{ int count = 0; // 定义一个容器大小为10 Food[] foods = new Food[10]; // 往容器放食物 public synchronized void push(Food food){ // 如果容器满了,等待,通知消费者消费 if(count==foods.length){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 没有满,生产食物,放到容器中。 foods[count] = food; count++; // 通知消费者消费 notifyAll(); } // 从容器中取食物 public synchronized void Take(){ if(count<=0){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 有食物则取食物 count--; // 通知生产者生产 notifyAll(); } }
// 生产者 static class ProductMan implements Runnable{ // 定义容器 FoodContainer foodContainer; public ProductMan(FoodContainer foodContainer){ this.foodContainer = foodContainer; } // 生产者生产食物 public void run() { //生产食物 for (int i = 0; i <20 ; i++) { foodContainer.push(new Food()); System.out.println(Thread.currentThread().getName()+": 生产了"+i+"只鸡"); } } }
// 消费者 static class Consumman implements Runnable{ // 定义容器 FoodContainer foodContainer; public Consumman(FoodContainer foodContainer){ this.foodContainer = foodContainer; } // 消费者消费食物 public void run() { //消费食物 for (int i = 0; i <20 ; i++) { System.out.println(Thread.currentThread().getName()+": 消费了"+i+"只鸡"); foodContainer.Take(); } } }
public static void main(String[] args) { // 启动生产者和消费者线程 FoodContainer foodContainer =new FoodContainer(); new Thread(new ProductMan(foodContainer)).start(); new Thread(new Consumman(foodContainer)).start(); }
结果:生产者生产到第10只鸡的时,告诉消费者来消费才继续生产。
注意: 容器属于公共资源,所以在对容器进行操作事一定要加上线程同步。 下一篇介绍 生产者与消费者的另一种实现方式 信号灯法。转载地址:http://pjhof.baihongyu.com/