class Buffer {
    static final int SIZE = 10;
    int[] array = new int[SIZE];
    int rear = 0;
    int front =0;

    synchronized void enqueue(int item) throws InterruptedException {
        while (front == (rear+1) % SIZE) {
            wait();
        }
        rear = (rear+1) % SIZE;
        array[rear] = item;
        notifyAll();
    }

    synchronized int dequeue() throws InterruptedException {
        while (front==rear) {
            wait();
        }
        int item = array[front];
        front = (front+1) % SIZE;
        notifyAll();
        return item;
    }

    public static void main(String[] args) throws InterruptedException {
        Buffer queue = new Buffer();
        int[] count = {0};
        new Thread(() -> {
            while(true) {
                try {
                    queue.enqueue(1);
                }
                catch (InterruptedException e) {}
            }
        }).start();
        new Thread(() -> {
            while (true) {
                try {
                    count[0] += queue.dequeue();
                }
                catch (InterruptedException e) {}
            }
        }).start();
        while (true) {
            Thread.sleep(1000);
            System.out.println(count[0]);
        }
    }
}