書本中的java實現生產者/消費者問題的代碼改良

以下代碼爲改良版。

public class ProducerConsumer {
    
    static final int N = 3; // constant giving the buffer size
    static producer p = new producer(); // instantiate a new producer thread
    static consumer c = new consumer( ); // instantiate a new consumer thread
    static our_monitor mon = new our_monitor( ); // instantiate a new monitor
    
    public static void main(String args[]) {
        p.start(); // start the producer thread
        c.start(); // start the consumer thread
    }

    static class producer extends Thread {
        
        public void run( ) {// run method contains the thread code
            int item;
            while (true) { //producer loop
                item = produce_item( );
                mon.insert(item);
            }
        }
        
        private int produce_item( ) { // actually produce
            return 0;
        }
    }

    static class consumer extends Thread {
        public void run( ) {//run method contains the thread code
            int item;
            while (true) { // consumer loop
                item = mon.remove( );
                consume_item (item);
            }
        }
        private void consume_item(int item) { 
        }// actually consume
        
    }

    static class our_monitor {// this is a monitor
        
        private int buffer[] = new int[N];
        private int count = 0, lo = 0, hi = 0; // counters and indices
        
        public 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 buffer
            
            System.out.println("Produce number:" + val + ",at position=" + hi+ "\r\n");
            
            hi = (hi + 1) % N; // slot to place next item in
            count = count + 1; // one more item in the buffer now
            notify(); // 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 buffer
            
            System.out.println("Consume number:" + val+ ",at position=" + lo + "\r\n");
            
            lo = (lo + 1) % N; // slot to fetch next item from
            count = count - 1; // one few items in the buffer
            notify( ); // if producer was sleeping, wake it up
            return 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 size
    static producer p = new producer(); // instantiate a new producer thread
    static consumer c = new consumer( ); // instantiate a new consumer thread
    static our_monitor mon = new our_monitor( ); // instantiate a new monitor
    
    public static void main(String args[]) {
        p.start(); // start the producer thread
        c.start(); // start the consumer thread
    }

    static class producer extends Thread {
        
        public void run( ) {// run method contains the thread code
            int item;
            while (true) { //producer loop
                item = produce_item( );
                mon.insert(item);
            }
        }
        
        private int produce_item( ) { // actually produce
            return 0;
        }
    }

    static class consumer extends Thread {
        public void run( ) {//run method contains the thread code
            int item;
            while (true) { // consumer loop
                item = mon.remove( );
                consume_item (item);
            }
        }
        private void consume_item(int item) { 
        }// actually consume
        
    }

    static class our_monitor {// this is a monitor
        
        private int buffer[] = new int[N];
        private int count = 0, lo = 0, hi = 0; // counters and indices
        
        public synchronized void insert(int val){
            if (count == N) go_to_sleep(); // if the buffer is full, go to sleep
            buffer[hi] = val; // insert an item into the buffer
            
            System.out.println("Produce number:" + val + ",at position=" + hi);
            
            hi = (hi + 1) % N; // slot to place next item in
            count = count + 1; // one more item in the buffer now
            if (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 sleep
            val = buffer [lo]; // fetch an item from the buffer
            
            System.out.println("Consume number:" + val+ ",at position=" + lo);
            
            lo = (lo + 1) % N; // slot to fetch next item from
            count = count - 1; // one few items in the buffer
            if (count == N - 1) notify( ); // if producer was sleeping, wake it up
            
            return 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.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章