现代操作系统书本中的java实现生产者/消费者问题的代码改良

本文主要是介绍现代操作系统书本中的java实现生产者/消费者问题的代码改良,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

以下代码为改良版。

public class ProducerConsumer {static final int N = 3; // constant giving the buffer sizestatic producer p = new producer(); // instantiate a new producer threadstatic consumer c = new consumer( ); // instantiate a new consumer threadstatic our_monitor mon = new our_monitor( ); // instantiate a new monitorpublic static void main(String args[]) {p.start(); // start the producer threadc.start(); // start the consumer thread}static class producer extends Thread {public void run( ) {// run method contains the thread codeint item;while (true) { //producer loopitem = produce_item( );mon.insert(item);}}private int produce_item( ) { // actually producereturn 0;}}static class consumer extends Thread {public void run( ) {//run method contains the thread codeint item;while (true) { // consumer loopitem = mon.remove( );consume_item (item);}}private void consume_item(int item) { }// actually consume}static class our_monitor {// this is a monitorprivate int buffer[] = new int[N];private int count = 0, lo = 0, hi = 0; // counters and indicespublic synchronized void insert(int val){if (count >= N) {go_to_sleep(count,hi,"Insert"); // if the buffer is full, go to sleep}buffer[hi] = val; // insert an item into the bufferSystem.out.println("Produce number:" + val + ",at position=" + hi+ "\r\n");hi = (hi + 1) % N; // slot to place next item incount = count + 1; // one more item in the buffer nownotify(); // if consumer was sleeping, wake it up}public synchronized int remove( ) {int val;if (count == 0) {go_to_sleep(count,lo,"Remove"); // if the buffer is empty, go to sleep}    val = buffer [lo]; // fetch an item from the bufferSystem.out.println("Consume number:" + val+ ",at position=" + lo + "\r\n");lo = (lo + 1) % N; // slot to fetch next item fromcount = count - 1; // one few items in the buffernotify( ); // if producer was sleeping, wake it upreturn val;}private void go_to_sleep(int count,int val,String action) { try{System.out.println("Current total number:" + count+ ",value=" + val+",Action=" + action+ "\r\n");wait( );} catch(InterruptedException exc){}}}
}
//Figure 2-35. A solution to the producer-consumer problem in Java,copy from <<Modern Operating System>> by Andrew S.Tanenbaum.

先前从书中摘抄的,似乎不能正确执行。

public class ProducerConsumer {static final int N = 3; // constant giving the buffer sizestatic producer p = new producer(); // instantiate a new producer threadstatic consumer c = new consumer( ); // instantiate a new consumer threadstatic our_monitor mon = new our_monitor( ); // instantiate a new monitorpublic static void main(String args[]) {p.start(); // start the producer threadc.start(); // start the consumer thread}static class producer extends Thread {public void run( ) {// run method contains the thread codeint item;while (true) { //producer loopitem = produce_item( );mon.insert(item);}}private int produce_item( ) { // actually producereturn 0;}}static class consumer extends Thread {public void run( ) {//run method contains the thread codeint item;while (true) { // consumer loopitem = mon.remove( );consume_item (item);}}private void consume_item(int item) { }// actually consume}static class our_monitor {// this is a monitorprivate int buffer[] = new int[N];private int count = 0, lo = 0, hi = 0; // counters and indicespublic synchronized void insert(int val){if (count == N) go_to_sleep(); // if the buffer is full, go to sleepbuffer[hi] = val; // insert an item into the bufferSystem.out.println("Produce number:" + val + ",at position=" + hi);hi = (hi + 1) % N; // slot to place next item incount = count + 1; // one more item in the buffer nowif (count == 1) notify(); // if consumer was sleeping, wake it up}public synchronized int remove( ) {int val;if (count == 0) go_to_sleep(); // if the buffer is empty, go to sleepval = buffer [lo]; // fetch an item from the bufferSystem.out.println("Consume number:" + val+ ",at position=" + lo);lo = (lo + 1) % N; // slot to fetch next item fromcount = count - 1; // one few items in the bufferif (count == N - 1) notify( ); // if producer was sleeping, wake it upreturn val;}private void go_to_sleep( ) { try{System.out.println("队列不能满足要求,挂起。。。\\n");wait( );} catch(InterruptedException exc){}}}
}
//Figure 2-35. A solution to the producer-consumer problem in Java.


这篇关于现代操作系统书本中的java实现生产者/消费者问题的代码改良的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/796735

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当