<<现代操作系统>>书本中的java实现生产者/消费者问题的代码改良
2017-05-26 15:21
816 查看
以下代码为改良版。
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;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;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代码实现多线程消费者生产者问题。
- 用java代码实现生产者和消费者的问题
- 生产者与消费者问题(java多线程实现)
- 生产者消费者问题的Java实现
- java多线程总结六:经典生产者消费者问题实现
- 哲学家进餐问题与生产者与消费者问题(java实现)
- 生产者消费者线程在Queue<T>中实现多线程同步
- java实现的生产者和消费者问题-涉及线程同步与通信
- 用多线程实现“生产者-消费者问题”(代码+实验报告)
- java多线程实现生产者与消费者问题
- java生产者和消费者问题之售票实现方法
- 经典线程同步问题(生产者&消费者)--Java实现
- 用JAVA实现多线程(生产者与消费者问题)
- 问题1:java中没有实现这种“byte a = 0xB2 --> String b = “B2””转换的简单实现需要自己实现。 答:自己编写的转换函数,思路将byte的高低4位分开,分别转换为对应的字符然后合成返回的字符串。 java 代码 1.
- Java 多线程 生产者与消费者问题测试代码
- 用JAVA 实现“生产者-消费者”问题
- 生产者消费者问题之Java线程实现
- 八皇后问题之回溯算法实现<java实现>
- java多线程总结六:经典生产者消费者问题实现
- 生产者-消费者问题(condition)Java实现