Producers and Consumers Examples
Using Buffer: All sources
This example of producers and consumers was taken from JPF Tutorial by Klaus Havelund.
PC.java is a very simple producer/consumer example with one Producer and one Consumer thread. Producer generates some objects of type AttrData and puts them into a shared buffer. Consumer takes the objects out of the buffer and stores them into its own array. The class has an additional method halt() that gets called by Producer that wants to stop working. It sets halted field of the Buffer to true. Method get() of the Buffer throws a HaltException when the buffer has been halted and emptied. When Producer is done it calls halt() method of the buffer to let Consumer know that it stopped working. When Consumer catches a HaltedException, it stops as well.Using LinkedQueue: SpEx-JML All sources
This example is taken from Doug Lea's book "Concurrent Programming in Java".