生产者消费者与发射器

时间:2015-09-09 02:55:38

标签: java multithreading

这是我遇到的面试问题。我在多线程编程中真的很天真。

这是一个问题:有三个主题:Producer,Consumer和Transmitter。 Producer和Transmitter共享一个名为ProduceBuf的缓冲区。 Consumer和Transmitter共享名为ConsumeBuf的缓冲区。 Producer每次都创建一个对象并将其添加到ProduceBuf中。 Transmitter从ProduceBuf中获取一个对象并对该对象执行一些操作然后将其放入ConsumeBuf中。消费者使用ConsumeBuf中的对象。 而ProduceBuf最多可以获得12个对象。 ConsumeBuf最多可以获得6个对象。

这是我的尝试:

public class AdvanceCP {

    public static void main(String[] args) {

         AdvanceCP ad = new AdvanceCP();
         ProduceBuf pb = ad.new ProduceBuf();
         ConsumeBuf cb = ad.new ConsumeBuf();

         Producer p = ad.new Producer(pb);
         Transmitter t = ad.new Transmitter(pb,cb);
         Consumer c = ad.new Consumer(cb);

         Thread tp = new Thread(p);
         Thread tt = new Thread(t);
         Thread tc = new Thread(c);

         tc.start();
         tt.start();
         tp.start();
    }

    class ProduceBuf {
        int index = 0;
        Integer[] buffer = new Integer[12];

        public synchronized void produce(){
            while(index == buffer.length){
                //buffer is full
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            int newInt = (int) (Math.random()*100);
            buffer[index] = newInt;
            System.out.println("Producing a new object."+newInt);
            index++;
            this.notify();
        }

        public synchronized Integer pop(){
            while(index==0) {
                try{
                    this.wait();
                } catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
            index--;
            System.out.println("Transmitting a new object."+buffer[index]);
            return buffer[index];
        }
    }


    class ConsumeBuf {
        int index = 0;
        Integer[] buffer = new Integer[6];

        public synchronized void push(Integer newint){
            while(index==buffer.length){
                //buffer is full
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            buffer[index] = 0-newint;
            System.out.println("Transmitted a new object."+buffer[index]);
            index++;

            this.notify();
        }

        public synchronized Integer pop(){
            while(index==0) {
                try{
                    this.wait();
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
            index--;
            System.out.println("Consuming a new object."+buffer[index]);
            return buffer[index];
        }
    }

    class Producer extends Thread{
        ProduceBuf pb =null;
        Producer(ProduceBuf p){
            pb = p;
        }
        public void run(){
            while(true) {
                pb.produce();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class Transmitter extends Thread{
        ProduceBuf pb = null;
        ConsumeBuf cb = null;
        Transmitter(ProduceBuf p,ConsumeBuf c){
            pb = p;
            cb  =c;
        }
        public void run(){
            while(true){
                cb.push(pb.pop());
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class Consumer extends Thread{
        ConsumeBuf cb = null;
        Consumer(ConsumeBuf c){
            cb=c;
        }
        public void run(){
            while(true){
            cb.pop();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

然而,控制台输出似乎不像多线程一样:

Producing a new object.43
Transmitting a new object.43
Transmitted a new object.-43
Consuming a new object.-43
Producing a new object.39
Transmitting a new object.39
Transmitted a new object.-39
Consuming a new object.-39
Producing a new object.98
Transmitting a new object.98
Transmitted a new object.-98
Consuming a new object.-98
Producing a new object.64
Transmitting a new object.64
Transmitted a new object.-64
Consuming a new object.-64
Producing a new object.52
Transmitting a new object.52
Transmitted a new object.-52
Consuming a new object.-52

所以我尝试调整这三个线程的睡眠时间。如果我将消费者的睡眠时间添加到500.该程序在做完某些事情后会停滞不前:

 Producing a new object.77
Transmitting a new object.77
Transmitted a new object.-77
Consuming a new object.-77
Producing a new object.83
Transmitting a new object.83
Transmitted a new object.-83
Producing a new object.46
Transmitting a new object.46
Transmitted a new object.-46
Producing a new object.28
Transmitting a new object.28
Transmitted a new object.-28
Producing a new object.72
Transmitting a new object.72
Transmitted a new object.-72
Producing a new object.25
Transmitting a new object.25
Transmitted a new object.-25
Producing a new object.94
Transmitting a new object.94
Transmitted a new object.-94
Producing a new object.63
Transmitting a new object.63
Producing a new object.64
Producing a new object.13
Producing a new object.14
Consuming a new object.-94
Producing a new object.63
Producing a new object.87
Producing a new object.80
Producing a new object.49
Producing a new object.85
Producing a new object.35
Producing a new object.16
Producing a new object.34
Producing a new object.14
Consuming a new object.-25
Consuming a new object.-72
Consuming a new object.-28
Consuming a new object.-46
Consuming a new object.-83

我不知道重点在哪里。作为多线程的初学者,我确信有一些我忽略的东西,我很感激你的帮助。

0 个答案:

没有答案