package methods_objects;

/**
 * @author SergeyI
 * @since 01.11.2017
 *
 * Here this is class show us how are working two threads in one queue.
 */
public class ProducerCustomer<E> {
    Node<E> node = new Node<>();
    Object lock = new Object();

    public void add(E o) {
        synchronized (lock) {
            if (node == null) {
                node = new Node<E>(o);
            } else if (node.next == null) {
                node.next = new Node(o);

            } else {
                Node<E> temp = node;
                node = node.next;
                add(o);
                node = temp;
            }
                lock.notify();
        }
    }

    public E get() {
        Node<E> temp;
        synchronized (lock) {
            temp = new Node<>();
            if (node != null) {
                temp = node;
                node = node.next;
            } else {
                System.out.println("any");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        return temp.object;
    }

    private class Node<E> {
        Node next;
        E object;

        public Node(E e) {
            object = e;
        }

        public Node() {
        }
    }


}

@@ -19,16 +19,46 @@
 jects;%0A%0A
+import java.util.LinkedList;%0A%0A
 /**%0A * @
@@ -95,16 +95,20 @@
 .2017%0A *
+ %3Cp%3E
 %0A * Here
@@ -219,463 +219,148 @@
     
-Node%3CE%3E node = new Node%3C%3E();%0A    Object lock = new Object();%0A%0A    public void add(E o) %7B%0A        synchronized (lock) %7B%0A            if (node == null) %7B%0A                node = new Node%3CE%3E(o);%0A            %7D else if (node.next == null) %7B%0A                node.next = new Node(o);%0A%0A            %7D else %7B%0A                Node%3CE%3E temp = node;%0A                node = node.next;%0A                add(o);%0A                node = temp;%0A            %7D%0A                lock
+LinkedList%3CE%3E list = new LinkedList%3C%3E();%0A%0A    public void add(E o) %7B%0A        synchronized (list) %7B%0A            list.add(o);%0A            list
 .not
@@ -416,20 +416,16 @@
     
-Node%3CE%3E temp
+Object e
 ;%0A  
@@ -445,19 +445,19 @@
 nized (l
-ock
+ist
 ) %7B%0A    
@@ -468,187 +468,29 @@
     
-temp = new Node%3C%3E();%0A            if (node != null) %7B%0A                temp = node;%0A                node = node.next;%0A            %7D else %7B%0A                System.out.println(%22any%22);
+if (list.isEmpty()) %7B
 %0A   
@@ -533,11 +533,11 @@
    l
-ock
+ist
 .wai
@@ -588,16 +588,17 @@
 eption e
+1
 ) %7B%0A    
@@ -614,16 +614,17 @@
        e
+1
 .printSt
@@ -679,213 +679,69 @@
     
-%7D%0A
     
-    return temp.object;%0A    %7D%0A%0A    private class Node%3CE%3E %7B%0A        Node next;%0A        E object;%0A%0A        public Node(E e) %7B%0A            object = e;%0A        %7D%0A%0A        public Node() %7B%0A        %7D%0A    %7D%0A
+e = list.remove(0);%0A        %7D%0A        return (E) e;%0A    %7D
 %0A%0A%7D%0A

task 1098