在同步缓冲区中添加多个值

时间:2015-10-29 14:25:31

标签: java multithreading buffer

所以我有一个Producer,Consumer和一个共享的Synchronized缓冲区。 生产者创建一个数字(保存在缓冲区中) 消费者可以猜测猜测数量。 一旦消费者猜对了,就会询问是否要再次播放,并且正确猜测的次数被保存在缓冲区中。

我的synchronizedBuffer类都错了。我甚至不知道从哪里开始实现第二个值。关于我应该怎么做的一点点暗示将非常感激。我不允许使用数组阻塞队列。

 //Class: Producer
    import java.util.Random;
    import java.util.concurrent.ThreadLocalRandom;

    public class Producer implements Runnable {
        private final static Random generator = new Random();
        private final Buffer sharedLocation;

        public Producer(Buffer shared) {
            sharedLocation = shared;
        }

        public void run() {

            try {
                int x = ThreadLocalRandom.current().nextInt(1, 10);
                Thread.sleep(generator.nextInt(3000)); // random sleep
                sharedLocation.set(x); // set value in buffer
                System.out.printf("\n", x);
            }
            catch (InterruptedException exception) {
                exception.printStackTrace();
            }

            System.out.println("Producer done producing");
        }
    }

     //Class: Consumer
     import java.util.Random;

    import java.util.concurrent.ThreadLocalRandom;

    public class Consumer implements Runnable {
        private final static Random generator = new Random();
        private final Buffer sharedLocation;

        public Consumer(Buffer shared) {
            sharedLocation = shared;
        }

        public void run() {

            int correct = 0;
            outerloop: for (int i = 1; i <= 5; i++) {
                try {
                    int x = ThreadLocalRandom.current().nextInt(1, 10);

                    Thread.sleep(generator.nextInt(2000));
                    if (x == sharedLocation.get()) {
                        System.out.println(x);
                        System.out.println("correct guess was " + x);
                        correct++;
                        sharedLocation.set(correct);
                        break outerloop;
                    }
                    System.out.print(x + "\n");

                } catch (InterruptedException exception) {
                    exception.printStackTrace();
                }

            }
        }

    }


    public class SynchronizedBuffer implements Buffer {
        private int buffer = -1;
        private boolean occupied = false;

        public synchronized void set(int value) throws InterruptedException {

            while (occupied) {

                System.out.println("Producer tries to write.");
                System.out.println("Consumer tries to guess");
                wait();
            } // end while

            buffer = value; 

            occupied = true;

            displayState("Producer writes " + buffer);


            notifyAll();
            buffer = value;

        } 


        public synchronized int get() throws InterruptedException {

            while (!occupied) {
            System.out.println("Consumer tries to guess.");
                displayState("Buffer empty. Consumer waits.");
                wait();
            } 
            occupied = false;


            notifyAll();

            return buffer;
        }


        public void displayState(String operation) {
            System.out.printf("%-40s%d\t\t%b\n", operation, buffer, occupied);
        }
    }

0 个答案:

没有答案