线程通信

    xiaoxiao2025-04-06  24

    线程通信

    使用wait()、notify()或者notifyAll()实现线程通信

    解决方式1:管程法

    并发协作模型“生产者/消费者模式”——>管程法生产者:负责生产数据的模块(这里的模块可能是:方法、对象、线程、进程)消费者:负责处理数据的模块(这里的模块可能是:方法、对象、线程、进程)缓冲区:消费者不能直接使用生产者的数据,它们之间有个“缓冲区”;生产者将生产好的数据放入“缓冲区”,消费者从“缓冲区”拿要处理的数据。 package com.study.cooperation; /** * 协作模型:生产者消费者实现方式一:管程法 * 借助缓冲区 * @author suzuya * */ public class CoTest01 { public static void main(String[] args) { SynContainer container = new SynContainer(); new Productor(container).start(); new Consumer(container).start(); } } //生产者 class Productor extends Thread{ SynContainer container; public Productor(SynContainer container) { this.container = container; } public void run() { //开始生产 for (int i = 1; i <= 100; i++) { System.out.println("生产——>第"+i+"个"); container.push(new Steamedbun(i)); } } } //消费者 class Consumer extends Thread{ SynContainer container; public Consumer(SynContainer container) { this.container = container; } public void run() { //消费 for (int i = 1; i <= 100; i++) { System.out.println("消费——>第"+container.pop().id+"个"); } } } //缓冲区 class SynContainer{ Steamedbun [] buns = new Steamedbun[10];//存储容器 int count = 0;//计数器 //存储 生产 public synchronized void push(Steamedbun bun) { //何时能生产 容器存在空间 //不能生产 if (count == buns.length) { try { this.wait();//线程阻塞 消费者通知生产解除 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } buns[count]=bun; count++; this.notifyAll();//存在产品了,可以唤醒对方消费 } //获取 public synchronized Steamedbun pop() { //何时消费 容器中是否存在数据 //没有数据 只能等待 if (count == 0) { try { this.wait();//线程阻塞 生产者通知消费解除 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } count--; Steamedbun bun = buns[count]; this.notifyAll();//存在空间了,可以唤醒对方生产 return bun; } } //产品 class Steamedbun{ int id; public Steamedbun(int id) { this.id = id; } }

    解决方式2:信号灯法

    并发协作模型“生产者/消费者模式”——>信号灯法使用标志切换 package com.study.cooperation; /** * 协作模型:生产者消费者实现方式二:信号灯法 * 借助标志位 * @author suzuya * */ public class CoTest02 { public static void main(String[] args) { Tv tv = new Tv(); new Player(tv).start(); new Watcher(tv).start(); } } //生产者 演员 class Player extends Thread{ Tv tv; public Player(Tv tv) { super(); this.tv = tv; } public void run() { for (int i = 0; i < 20; i++) { if (i % 2 == 0) { this.tv.play("电影"); } else { this.tv.play("广告"); } } } } //消费者 观众 class Watcher extends Thread{ Tv tv; public Watcher(Tv tv) { super(); this.tv = tv; } public void run() { for (int i = 0; i < 20; i++) { tv.watch(); } } } //同一资源 电视 class Tv{ String voice; //信号灯 //T 表示演员表演 观众等待 //F 表示观众观看 演员等待 boolean flag = true; //表演 public synchronized void play(String voice) { //演员等待 if (!flag) { try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("表演了:"+voice); this.voice = voice; this.flag = !flag; this.notifyAll(); } public synchronized void watch() { //观众等待 //演员等待 if (flag == true) { try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("听到了:"+voice); this.flag = !flag; this.notifyAll(); } }
    最新回复(0)