本文主要是介绍现代操作系统书本中的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实现生产者/消费者问题的代码改良的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!