package ru.job4j.simpleBlockingQueue;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import java.util.LinkedList;
import java.util.Queue;

@ThreadSafe
public class SimpleBlockingQueue<T> {

    private final Object lock = new Object();

    @GuardedBy("this")
    private final Queue<T> queue = new LinkedList<>();

    //Вставляет элемент в конец очереди
    public synchronized void offer(T value) {
        this.queue.offer(value);
    }

    // возвращает и удаляет головной элемент
    public synchronized T poll() {
            return this.queue.poll();
         }

    public void doSomething(T value) throws InterruptedException {
        synchronized (this.lock) {
            if (queue.peek()==null) {
                offer(value);
                lock.wait();
            }
        }
    }

    public synchronized void changeBlock() {
        synchronized (this.lock) {
            if (queue.peek()!=null) {
                this.poll();
            }
            lock.notify();
        }
    }

    public static void main (String[] args) throws InterruptedException {

        // producer блокирует если очередь заполнена
        // до тех пор пока Cunsomer не извлечет данные из очередиы
        final SimpleBlockingQueue sbq = new SimpleBlockingQueue();

        //customer
        Thread cunsomer =  new Thread() {
            @Override
            public void run() {
                try {
                    sbq.doSomething(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        //producer
        Thread producer = new Thread() {
            @Override
            public void run() {
                sbq.changeBlock();
            }
        };

        cunsomer.start();
        producer.start();

    }

}

@@ -300,30 +300,29 @@
 this%22)%0A    p
-rivate
+ublic
  final Queue
@@ -360,16 +360,17 @@
 %0A%0A    //
+ 
 %D0%92%D1%81%D1%82%D0%B0%D0%B2%D0%BB%D1%8F%D0%B5
@@ -569,20 +569,16 @@
         
-    
 return t
@@ -591,29 +591,24 @@
 eue.poll();%0A
-     
     %7D%0A%0A    p
@@ -609,24 +609,37 @@
 %0A    public 
+synchronized 
 void doSomet
@@ -721,36 +721,44 @@
 ) %7B%0A            
-if (
+while (this.
 queue.peek()==nu
@@ -755,32 +755,156 @@
 peek()==null) %7B%0A
+                try %7B%0A                    System.out.println(String.format(%22%25s wait%22, Thread.currentThread().getId()));%0A    
                 
@@ -929,24 +929,33 @@
             
+    this.
 lock.wait();
@@ -959,33 +959,242 @@
 ();%0A            
-%7D
+    %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A            System.out.println(String.format(%22%25s userfull work%22, Thread.currentThread().getId()));
 %0A        %7D%0A    %7D
@@ -1291,33 +1291,87 @@
     
-if (queue.peek()!=null) %7B
+System.out.println(String.format(%22%25s enable%22, Thread.currentThread().getId()));
 %0A   
@@ -1379,25 +1379,16 @@
         
-    this.
 poll();%0A
@@ -1399,30 +1399,21 @@
         
-%7D%0A            
+this.
 lock.not
@@ -1541,16 +1541,22 @@
 %D0%BE%D0%BA%D0%B8%D1%80%D1%83%D0%B5%D1%82 
+%D0%BF%D0%BE%D1%82%D0%BE%D0%BA 
 %D0%B5%D1%81%D0%BB%D0%B8 %D0%BE%D1%87%D0%B5
@@ -1635,17 +1635,16 @@
  %D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8
-%D1%8B
 %0A       
@@ -2241,21 +2241,23 @@
 r.start();%0A%0A
+%0A%0A
     %7D%0A%0A%7D%0A

task #187595