@Override public boolean add(Work work) { boolean added = false; if (workBlockingQueue.hasWaitingConsumer()) { added = workBlockingQueue.add(work); } else { try { workBlockingQueue.transfer(work); added = true; } catch (InterruptedException e) { e.printStackTrace(); } } return added; }
@Override public void run() { for (int i = 1; i <= 9; i++) { if (Thread.currentThread().isInterrupted()) { return; } System.out.format("Элемент 'ShareItem-%d' добавлен%n", i); queue.offer(new ShareItem("ShareItem-" + i, i)); try { Thread.sleep(100); } catch (InterruptedException e) { } if (queue.hasWaitingConsumer()) { System.out.println("Consumer в ожидании!"); } } }
@Override public Work take() throws InterruptedException { return workBlockingQueue.take(); }