@Test public void testGet() { GuardedQueue g = new GuardedQueue(); ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.submit(() -> value = g.get()); executorService.submit(() -> g.put(Integer.valueOf(10))); executorService.shutdown(); try { executorService.awaitTermination(30, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } assertEquals(Integer.valueOf(10), value); }
public synchronized Integer get() { while (sourceList.isEmpty()) { try { LOGGER.info("waiting"); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } LOGGER.info("getting"); return sourceList.peek(); }
GuardedQueue { public synchronized Integer get() { while (sourceList.isEmpty()) { try { LOGGER.info("waiting"); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } LOGGER.info("getting"); return sourceList.peek(); } }
GuardedQueue { public synchronized Integer get() { while (sourceList.isEmpty()) { try { LOGGER.info("waiting"); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } LOGGER.info("getting"); return sourceList.peek(); } GuardedQueue(); }
GuardedQueue { public synchronized Integer get() { while (sourceList.isEmpty()) { try { LOGGER.info("waiting"); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } LOGGER.info("getting"); return sourceList.peek(); } GuardedQueue(); synchronized Integer get(); synchronized void put(Integer e); }
GuardedQueue { public synchronized Integer get() { while (sourceList.isEmpty()) { try { LOGGER.info("waiting"); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } LOGGER.info("getting"); return sourceList.peek(); } GuardedQueue(); synchronized Integer get(); synchronized void put(Integer e);  }