@@ -0,0 +1,1763 @@
+package producerConsumer;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Queue;%0A%0A/**%0A * Tread consumer. Extracts data from shared queue.%0A * @param %3CE%3E - any.%0A */%0Apublic class Consumer%3CE%3E implements Runnable %7B%0A    /**%0A     * Shared data-structure.%0A     */%0A    private final Queue%3CE%3E sharedQueue;%0A    /**%0A     * For save results which get from queue.%0A     */%0A    private List%3CE%3E result = new ArrayList%3C%3E();%0A%0A    /**%0A     * Getter for result data.%0A     * @return result data.%0A     */%0A    public List%3CE%3E getResult() %7B%0A        return result;%0A    %7D%0A%0A    /**%0A     * Default constructor.%0A     * @param sharedQueue init shared queue for extract.%0A     */%0A    public Consumer(Queue sharedQueue) %7B%0A        this.sharedQueue = sharedQueue;%0A    %7D%0A%0A    /**%0A     * Get elements from queue and add in result list.%0A     */%0A    @Override%0A    public void run() %7B%0A        while (true) %7B%0A            try %7B%0A                result.add(consume());%0A                //System.out.println(sharedQueue.size());%0A                Thread.sleep(1);%0A            %7D catch (InterruptedException ex) %7B%0A                ex.printStackTrace();%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Get next element from queue.%0A     * If queue is empty call wait() and free lock object(shared queue).%0A     * @return Element if queue not empty.%0A     * @throws InterruptedException - from wait.%0A     */%0A    private E consume() throws InterruptedException %7B%0A        while (sharedQueue.isEmpty()) %7B%0A            synchronized (sharedQueue) %7B%0A                sharedQueue.wait();%0A            %7D%0A        %7D%0A%0A        //If not empty consume element and notify waiting producer%0A        synchronized (sharedQueue) %7B%0A            sharedQueue.notifyAll();%0A            return sharedQueue.poll();%0A        %7D%0A    %7D%0A%7D%0A

add producer-consumer pattern