Skip to content

Instantly share code, notes, and snippets.

@chathurangat
Created December 19, 2017 17:45
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/9b412ed7473bde09380ae5ecdda2b307 to your computer and use it in GitHub Desktop.
Save chathurangat/9b412ed7473bde09380ae5ecdda2b307 to your computer and use it in GitHub Desktop.
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