... java.util.concurrent.ExecutorService; 5   import java.util.concurrent.Executors;
 6   import java.util.concurrent.TimeUnit;
 7
 8   public class BlockingBufferTest {
 9      public static void main(String[] args) throws InterruptedException {
10         // create new thread pool
11         ExecutorService executorService = Executors.newCachedThreadPool();
12
13         // create BlockingBuffer to store ints
14         Buffer sharedLocation = new BlockingBuffer();
15
16         executorService.execute(new Producer(sharedLocation));
17         executorService.execute(new Consumer(sharedLocation));
18
19         executorService.shutdown();
20         executorService.awaitTermination(1, TimeUnit.MINUTES);
21      }
22   }

 Producer writes 1 Buffer cells occupied: 1 Consumer reads 1 Buffer cells occupied: 0 Producer writes 2 Buffer cells ...

Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.