@@ -0,0 +1,2580 @@
+package producerConsumer;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Queue;%0A%0A/**%0A * Tread producer. Add data in shared queue.%0A * @param %3CE%3E - any.%0A */%0Apublic class Producer%3CE%3E implements Runnable %7B%0A    /**%0A     * Data roe add.%0A     */%0A    private E%5B%5D src;%0A    /**%0A     * Shared data-structure.%0A     */%0A    private final Queue%3CE%3E sharedQueue;%0A%0A    /**%0A     * Default constructor.%0A     * @param sharedQueue data structure for shared.%0A     * @param src first ste data for add.%0A     */%0A    public Producer(final Queue sharedQueue, E%5B%5D src) %7B%0A        this.sharedQueue = sharedQueue;%0A        this.src = src;%0A    %7D%0A%0A    /**%0A     * For load new data for add in queue.%0A     * @param src data for add.%0A     */%0A    public void setSrc(E%5B%5D src) %7B%0A        this.src = src;%0A    %7D%0A%0A    /**%0A     * Produce first src array, and puts on hold more data.%0A     */%0A    @Override%0A    public void run() %7B%0A        E%5B%5D tempSrc = src;%0A        forEachProduce();%0A        nextSetProduce(tempSrc);%0A%0A    %7D%0A%0A    /**%0A     * Check has been added last src data.%0A     * If true - sleep 3 seconds, else add elements in queue.%0A     * And recursive call.%0A     * @param tempSrc reference on temp src object.%0A     */%0A    private void nextSetProduce(E%5B%5D tempSrc) %7B%0A        while (Arrays.equals(tempSrc, src)) %7B%0A            try %7B%0A                // %D1%82%D1%83%D1%82 %D0%BC%D0%BD%D0%B5 %D0%BD%D0%B0 %D0%BC%D0%BE%D0%BD%D0%B8%D1%82%D0%BE%D1%80 %D0%B2%D0%BB%D0%B8%D1%8F%D1%82%D1%8C %D0%BD%D0%B5 %D0%BD%D0%B0%D0%B4%D0%BE %D1%82%D0%B0%D0%BA %D1%87%D1%82%D0%BE sleep %D0%B2 %D1%81%D0%B0%D0%BC%D1%8B%D0%B9 %D1%80%D0%B0%D0%B7.%0A                Thread.currentThread().sleep(10);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A%0A        forEachProduce();%0A        tempSrc = src;%0A        nextSetProduce(tempSrc);%0A    %7D%0A%0A    /**%0A     * For each src array for add.%0A     */%0A    private void forEachProduce() %7B%0A        for (E e : src) %7B%0A            try %7B%0A                produce(e);%0A            %7D catch (InterruptedException ex) %7B%0A                ex.printStackTrace();%0A            %7D%0A%0A        %7D%0A    %7D%0A%0A    /**%0A     * If queue is full current thread wait and free lock object.%0A     * Else add element in queue, holds lock object.%0A     * @param element for add.%0A     * @throws InterruptedException from wait().%0A     */%0A    private void produce(E element) throws InterruptedException %7B%0A        //wait if queue is full%0A        while (sharedQueue.size() == 10) %7B%0A            synchronized (sharedQueue) %7B%0A                sharedQueue.wait();%0A            %7D%0A        %7D%0A%0A        //producing element and notify consumers%0A        synchronized (sharedQueue) %7B%0A            sharedQueue.add(element);%0A            //System.out.println(sharedQueue.size());%0A            sharedQueue.notifyAll();%0A        %7D%0A    %7D%0A%7D%0A

add producer-consumer pattern