Skip to content

Instantly share code, notes, and snippets.

@chathurangat
Created December 19, 2017 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chathurangat/296867fbc3a2157a992410b06967e635 to your computer and use it in GitHub Desktop.
Save chathurangat/296867fbc3a2157a992410b06967e635 to your computer and use it in GitHub Desktop.
class Consumer extends Thread
{
private Integer numOfItemsToBeConsumed = 10;
private Queue<String> sharedQueue;
Consumer(Queue<String> sharedQueue)
{
this.sharedQueue = sharedQueue;
}
public void run()
{
System.out.println("consumer started ");
synchronized (sharedQueue) {
while (!sharedQueue.isEmpty() && numOfItemsToBeConsumed != 0) {
System.out.println("consuming " + sharedQueue.poll());
numOfItemsToBeConsumed--;
if (sharedQueue.isEmpty()) {
try {
System.out.println("consumer waits till producer produces items ");
Thread.sleep(2000);
sharedQueue.notify();
sharedQueue.wait();
} catch (InterruptedException e) {
System.out.println("Consumer: waiting thread interrupted ");
}
}
}
//notifying all the threads that are waiting on the shared queue
sharedQueue.notifyAll();
System.out.println("Consumer Completed (Nothing to be consumed) : remaining items ["+numOfItemsToBeConsumed+"]");
}
synchronized (this) {
//notify all the threads that are waited on the Consumer
notifyAll();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment