package ru.apetrov.producerCustomer;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by Andrey on 26.07.2017.
 * @param <E> type
 */
public class ProducerCustomer<E> {

    /**
     * Очередь которую будем блокировать.
     */
    private final Queue<E> queue;

    /**
     * переключатель выполнения потоков.
     */
    private boolean switcher;

    /**
     * Конструктор.
     */
    public ProducerCustomer() {
        this.queue = new LinkedList<E>();
        this.switcher = false;
    }

    /**
     * Добавление элемента в очередь.
     * @param element елемент
     */
    public void add(E element) {
        synchronized (this.queue) {
            while (this.switcher) {
                try {
                    this.queue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
<<<<<<< HEAD
            this.queue.add(element);
            System.out.printf("add element %s\n", element);
=======
        }
        this.queue.add(element);
        System.out.printf("add element %s\n", element);

        synchronized (this.queue) {
>>>>>>> f8ba17873297ab614cfd50019999138caddd09f2
            this.queue.notify();
            this.switcher = true;
        }
    }

    /**
     * Вывод елемента из очереди.
     */
    public void get() {
        synchronized (this.queue) {
            while (!this.switcher || this.queue.peek() == null) {
                try {
                    this.queue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
<<<<<<< HEAD
            System.out.printf("get %s\n", this.queue.poll());
=======
        }
        System.out.printf("get %s\n", this.queue.poll());

        synchronized (this.queue) {
>>>>>>> f8ba17873297ab614cfd50019999138caddd09f2
            this.queue.notify();
            this.switcher = false;
        }
    }

    /**
     * Main.
     * @param args args
     */
    public static void main(String[] args) {
        ProducerCustomer<Integer> prodCust = new ProducerCustomer<>();
        Thread producer = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    prodCust.add(i);
                }
            }
        });

        Thread customer = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    prodCust.get();
                }
            }
        });

        producer.start();
        customer.start();
    }
}
@@ -883,37 +883,24 @@
           %7D%0A
-%3C%3C%3C%3C%3C%3C%3C HEAD%0A
             
@@ -988,201 +988,8 @@
 t);%0A
-=======%0A        %7D%0A        this.queue.add(element);%0A        System.out.printf(%22add element %25s%5Cn%22, element);%0A%0A        synchronized (this.queue) %7B%0A%3E%3E%3E%3E%3E%3E%3E f8ba17873297ab614cfd50019999138caddd09f2%0A
     
@@ -1433,21 +1433,8 @@
   %7D%0A
-%3C%3C%3C%3C%3C%3C%3C HEAD%0A
     
@@ -1495,170 +1495,8 @@
 ));%0A
-=======%0A        %7D%0A        System.out.printf(%22get %25s%5Cn%22, this.queue.poll());%0A%0A        synchronized (this.queue) %7B%0A%3E%3E%3E%3E%3E%3E%3E f8ba17873297ab614cfd50019999138caddd09f2%0A
     

update 6.4.1