package ru.apetrov.producerCustomer;

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

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

    private Queue<E> queue;

    private boolean isLock = false;

    public ProducerCustomer() {
        this.queue = new LinkedList<E>();
    }

    public void add(E element) {
        synchronized (this.queue) {
            while (this.isLock) {
                try {
                    this.queue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        this.queue.add(element);
        System.out.printf("add element %s\n", element);

        synchronized (this.queue) {
            this.queue.notify();
            this.isLock = true;
        }
    }

    public void get() {
        synchronized (this.queue) {
            while (!this.isLock || this.queue.peek() == null) {
                try {
                    this.queue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.printf("get %s\n", this.queue.poll());

        synchronized (this.queue) {
            this.queue.notify();
            this.isLock = false;
        }
    }

    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();
    }
}
@@ -125,16 +125,35 @@
 7.2017.%0A
+ * @param %3CE%3E type%0A
  */%0Apubl
@@ -184,16 +184,74 @@
 r%3CE%3E %7B%0A%0A
+    /**%0A     * %D0%9E%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C %D0%BA%D0%BE%D1%82%D0%BE%D1%80%D1%83%D1%8E %D0%B1%D1%83%D0%B4%D0%B5%D0%BC %D0%B1%D0%BB%D0%BE%D0%BA%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C.%0A     */%0A
     priv
@@ -254,16 +254,22 @@
 private 
+final 
 Queue%3CE%3E
@@ -285,121 +285,323 @@
     
-private boolean isLock = false;%0A%0A    public ProducerCustomer() %7B%0A        this.queue = new LinkedList%3CE%3E();%0A    %7D%0A
+/**%0A     * %D0%BF%D0%B5%D1%80%D0%B5%D0%BA%D0%BB%D1%8E%D1%87%D0%B0%D1%82%D0%B5%D0%BB%D1%8C %D0%B2%D1%8B%D0%BF%D0%BE%D0%BB%D0%BD%D0%B5%D0%BD%D0%B8%D1%8F %D0%BF%D0%BE%D1%82%D0%BE%D0%BA%D0%BE%D0%B2.%0A     */%0A    private boolean switcher;%0A%0A    /**%0A     * %D0%9A%D0%BE%D0%BD%D1%81%D1%82%D1%80%D1%83%D0%BA%D1%82%D0%BE%D1%80.%0A     */%0A    public ProducerCustomer() %7B%0A        this.queue = new LinkedList%3CE%3E();%0A        this.switcher = false;%0A    %7D%0A%0A    /**%0A     * %D0%94%D0%BE%D0%B1%D0%B0%D0%B2%D0%BB%D0%B5%D0%BD%D0%B8%D0%B5 %D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%D0%B0 %D0%B2 %D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C.%0A     * @param element %D0%B5%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%0A     */
 %0A   
@@ -690,22 +690,24 @@
 e (this.
-isLock
+switcher
 ) %7B%0A    
@@ -1073,22 +1073,24 @@
    this.
-isLock
+switcher
  = true;
@@ -1099,32 +1099,82 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * %D0%92%D1%8B%D0%B2%D0%BE%D0%B4 %D0%B5%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%D0%B0 %D0%B8%D0%B7 %D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8.%0A     */%0A
     public void 
@@ -1242,22 +1242,24 @@
  (!this.
-isLock
+switcher
  %7C%7C this
@@ -1627,14 +1627,16 @@
 his.
-isLock
+switcher
  = f
@@ -1654,24 +1654,77 @@
    %7D%0A    %7D%0A%0A
+    /**%0A     * Main.%0A     * @param args args%0A     */%0A
     public s

update 6.4.1