Created
December 19, 2017 18:42
-
-
Save chathurangat/296867fbc3a2157a992410b06967e635 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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