Created
December 19, 2017 17:45
-
-
Save chathurangat/9b412ed7473bde09380ae5ecdda2b307 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 Producer extends Thread | |
{ | |
private Integer numOfItemsToBeProduced = 10; | |
private int maxQueueSize = 5; | |
private Queue<String> sharedQueue; | |
Producer(Queue<String> sharedQueue) | |
{ | |
this.sharedQueue = sharedQueue; | |
} | |
public void run() | |
{ | |
synchronized (sharedQueue) { | |
while (sharedQueue.size() <= maxQueueSize && numOfItemsToBeProduced != 0) | |
{ | |
String item = "item " + numOfItemsToBeProduced; | |
System.out.println("producing " + item); | |
sharedQueue.add(item); | |
numOfItemsToBeProduced--; | |
if (sharedQueue.size() == maxQueueSize) { | |
System.out.println("queue is full. notifying the consumer "); | |
try { | |
Thread.sleep(2000); | |
sharedQueue.notify(); | |
sharedQueue.wait(); | |
} catch (InterruptedException e) { | |
System.out.println("sleeping thread get interrupted "); | |
} | |
} | |
} | |
sharedQueue.notifyAll(); | |
System.out.println("Producer Completed (Nothing to be produced) : remaining items ["+numOfItemsToBeProduced+"]"); | |
} | |
synchronized (this) { | |
//notify all the threads that are waited on the Producer | |
notifyAll(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment