操作系统作业—— 请根据自己的实际情况选择一种语言的进程/线程同步工具进行学习,并尝试使用该工具实现睡觉的理发师问题的解决方案
2018-12-14 20:59
489 查看
版权声明:Zun版权所有,请勿盗用 https://blog.csdn.net/weixin_42297075/article/details/85009487
请根据自己的实际情况选择一种语言的进程/线程同步工具进行学习,并尝试使用该工具实现睡觉的理发师问题的解决方案
Barber类
[code] public class Barber implements Runnable { Barbershop barbershop; int index; @Override public void run() { // TODO Auto-generated method stub try { barbershop.simulate(index); }catch(InterruptedException e){ e.printStackTrace(); } } public Barber (Barbershop bs,int i) { this.barbershop=bs; index=i; } }
Barbershop类
[code]import java.util.concurrent.Semaphore; public class Barbershop { static int customer=0;//当前顾客数 static int MAX=5;//提供的让顾客等待的椅子数 static int busy=0;//理发师是否忙碌标识 static Semaphore mutex=new Semaphore(1);//信号量,只允许一个顾客占用理发师 public synchronized boolean isFull() { //判断是否为满 if(customer==MAX) { return true; } return false; } public synchronized boolean isEmpty() { // 判断是否为空 if(customer==0) { return true; } return false; } public void simulate(int index) throws InterruptedException { // 模拟函数 System.out.println("customer "+index+" come"); customer++; if(isFull()) { // 如果为满顾客离开 System.out.println("no chair for customer,"+"customer "+index+" left"); customer--; } else { if(busy==1) { // 如果理发师忙碌,顾客等待 System.out.println("customer "+index+" is waiting for the barber"); } mutex.acquire(); // 信号量减,防止其他进程再进入 synchronized (this) { // 同步 while (busy==1) { wait(); // 有人理发等待 } } if(customer==1) { // 只有一个顾客 System.out.println("there is only customer :customer "+index+" in the barbershop,the barber is working "); } busy=1; System.out.println("custoemr "+index+" is having a haircut"); Thread.sleep(1000); //理发时间1000毫秒 System.out.println("customer "+index+" left"); customer--; mutex.release(); // 释放资源 synchronized(this) { // 同步并唤醒 busy=0; notify(); } if(customer==0) { // 没有顾客,理发师睡觉 System.out.println("the barber start to sleep"); } } } public static void main(String[] args) throws InterruptedException { Barbershop barbershop= new Barbershop(); for (int i =1;i<=10;i++) { // 假设一共有10个顾客 new Thread(new Barber(barbershop,i)).start(); Thread.sleep((int)(600-Math.random()*500)); } } }
运行结果:
相关文章推荐
- iOS开发工具-如何使用网络封包分析工具Charles,通过配置proxy对http、https、tcp、udp 等协议的请求响应过程交互信息进行分析、判断、解决我们移动开发中的遇到的各种实际问题。
- 操作系统(三)同步进程问题--理发师的睡觉问题
- 雷观(十一):接私活有利有弊,我们应该根据自己的实际情况选择接私活
- 雷观(十一):接私活有利有弊,我们应该根据自己的实际情况选择接私活
- 3.第三单元任务二实训:使用作业提交系统提交Java作业时 ,需要输入Java源代码文件名和自己的邮箱,提交前对Java文件名以及邮箱进行有效检查。编写程序实现对输入的Java源文件名以及邮箱有效性的
- Java对文件读写的操作详解 - pwh309315228的博客 Java中文件读写操作的作用是什么? 回答这个问题时应该先想到的是Java只是一门语言,我们的一种使用工具而已,这样答案就明晰了,就是将外来的各种数据写入到某一个文件中去,用以保存下来;或者从文...
- 雷观(十一):接私活有利有弊,我们应该根据自己的实际情况选择接私活
- 动态调用WebService,首先声明:这篇文章是我从网上学习来的,根据自己实际的情况写的,只给自己看的!
- 动态调用WebService,首先声明:这篇文章是我从网上学习来的,根据自己实际的情况写的,只给自己看的!
- 大龄程序员的发展方向应该根据自己的实际情况选择做管理还是技术
- 好开心呀,能用自己学习知识去做作业了,也算是解决一些问题吧。操作系统实践作业:短作业优先(SJF)和先来先服务算法(FCFS)
- 雷观(十一):接私活有利有弊,我们应该根据自己的实际情况选择接私活
- QT使用MySql的配置(使用addLibraryPath载入插件),编译QT的MySql驱动问题及解决方案(自己使用libmysql.lib进行编译mysql.pro,万不得已可以查看Makefile.Debug以解决问题)
- [Windows 监控]使用windows自带的工具去对某一进程实现监控,并取得这一进程对系统的资源使用情况
- 自己做的关于select工具根据属性进行选择
- MFC 对于使用自己的线进行启动其他进程的虚拟地址映射问题
- 安卓自动化测试工具MonkeyRunner之使用ID进行参数化,以及List选择某项和弹出框点击确定的写法
- 如何实现在客户端,使CheckBox按照选择的顺序进行排序--来自博问的问题
- 【Java学习笔记】使用Collator进行本地化语言的排序
- hadoop学习之-使用ODCH工具实现oralce外部表访问HDFS数据文件