您的位置:首页 > 编程语言 > Java开发

<<现代操作系统>>书本中的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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: