操作系统多线程例子--吃水果问题
来源:互联网 发布:相册音乐制作软件 编辑:程序博客网 时间:2024/04/29 23:17
import java.util.concurrent.locks.*;class Resource // 封装水果资源,提供放水果和吃水果方法。{private String name;private static int ap = 0; // 苹果信号,0表示无,1表示有private static int or = 0; // 橘子信号,0表示无,1表示有private static int pz = 0; // 盘子有无水果信号,0表示无,1表示有Lock lock = new ReentrantLock(); // 锁对象Condition fmther_lock = lock.newCondition();// 爸妈共用监视器,可以唤醒爸妈其中一个放水果Condition son_lock = lock.newCondition(); // 儿子监视器Condition daughter_lock = lock.newCondition(); // 女儿监视器// 放水果,i=1表示是爸爸,i=2表示是妈妈void put(String name, int i) {lock.lock();try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}try {while (pz == 1) // 盘子有水果{try {fmther_lock.await();} catch (Exception e) {e.printStackTrace();}}if (i == 1) // 爸爸{ap = 1;pz = 1;this.name = name; // 此处赋值防止线程中断名字不符(错乱)System.out.println(this.name + "放苹果");son_lock.signal();} else if (i == 2) // 妈妈{or = 1;pz = 1;this.name = name; // 此处赋值防止线程中断名字不符(错乱)System.out.println(this.name + "放橘子");daughter_lock.signal();}} finally {lock.unlock();}}// 吃水果,i=1表示是儿子,i=2表示是女儿void eat(String name, int i) {lock.lock();try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}try {while (i == 1) // 儿子{while (ap == 0) {try {son_lock.await();} catch (Exception e) {e.printStackTrace();}}ap = 0;pz = 0;this.name = name; // 此处赋值防止线程中断名字不符(错乱)System.out.println(this.name + "吃苹果");fmther_lock.signal();}while (i == 2) // 女儿{while (or == 0) {try {daughter_lock.await();} catch (Exception e) {e.printStackTrace();}}or = 0;pz = 0;this.name = name; // 此处赋值防止线程中断名字不符(错乱)System.out.println(this.name + "吃橘子");fmther_lock.signal();}} finally {lock.unlock();}}}class FatherPut implements Runnable {private Resource r;FatherPut(Resource r) {this.r = r;}public void run() {while (true) {r.put("爸爸", 1);}}}class MotherPut implements Runnable {private Resource r;MotherPut(Resource r) {this.r = r;}public void run() {while (true) {r.put("妈妈", 2);}}}class SonEat implements Runnable {private Resource r;SonEat(Resource r) {this.r = r;}public void run() {while (true) {r.eat("儿子", 1);}}}class DaughterEat implements Runnable {private Resource r;DaughterEat(Resource r) {this.r = r;}public void run() {while (true) {r.eat("女儿", 2);}}}public class EatFruit {public static void main(String[] args) {Resource r = new Resource();FatherPut fa = new FatherPut(r);MotherPut mo = new MotherPut(r);SonEat son = new SonEat(r);DaughterEat dau = new DaughterEat(r);Thread t0 = new Thread(fa);Thread t1 = new Thread(mo);Thread t2 = new Thread(son);Thread t3 = new Thread(dau);t0.start();t1.start();t2.start();t3.start();}}
阅读全文
1 0
- 操作系统多线程例子--吃水果问题
- 多线程练习----吃水果问题
- 多线程_吃水果问题
- 操作系统 吃水果问题的P V 操作原语
- 吃水果问题,c++版
- 吃水果问题的模拟程序
- 在PV操作中会有一个经典的例子就是爸爸妈妈儿子女儿吃水果问题,爸爸妈妈向盘子里放入水果,儿子女儿向盘子取水果。然而爸爸只向盘子放苹果,妈妈只向盘子放橘子,女儿只能吃苹果,儿子只能吃橘子。并且盘子里只能
- 用pv操作实现 吃水果问题(C++语言)
- PV操作解决吃水果问题(Java实现)
- 水果不可乱吃
- hdoj 1205 吃水果
- 夏季吃水果常识
- 脂肪肝吃什么水果
- jlh吃水果
- 多线程---和尚吃馒头问题
- 生什么病,吃什么水果
- 生什么病,吃什么水果
- 生什么病吃什么水果
- ACM算法竞赛预处理模版
- 分页
- tensorflow android 移植
- 安装arm-linux-gcc交叉编译工具
- TPshop的安装设置
- 操作系统多线程例子--吃水果问题
- IMU标定算法流程
- 单例模式的七种写法
- windows命令行工具cmder
- 操作系统多线程例子--生产者与消费者
- 线程池模拟实现
- linux初学者-普通磁盘分区篇
- 两道最近收到某好公司的笔试题。
- 数据结构之红黑树