<<现代操作系统>>书本中的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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值