This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "notifyAll()" Watch "notifyAll()" New topic


Sagar kanchi
Ranch Hand

Joined: Oct 07, 2005
Posts: 50
The code is as follows
class CubbyHole{
int item;static boolean isItemSet;
public synchronized void get(){
if (isItemSet==false){
try{ this.wait();}
catch (InterruptedException ie){System.out.println("InterruptedException occured in get() of ubbyHole : "+ie);}}
System.out.println(Thread.currentThread().getName()+" Got : "+item);isItemSet = false; notifyAll();}

public synchronized void put(int item)
{if (isItemSet==true){
try{ this.wait();}
catch (InterruptedException ie){
System.out.println("InterruptedException occured in get() of CubbyHole : "+ie);}}
this.item = item; System.out.println("put : "+item);
isItemSet = true;notifyAll();}}
class Producer extends Thread
{CubbyHole c;
Producer(CubbyHole c){
this.c = c;
public void run(){
int i=0;
class Consumer extends Thread{
CubbyHole c;
String name;
Consumer(CubbyHole c,String name){
this.c = c;
public void run(){
class Test {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer pro = new Producer(c);
Consumer con1 = new Consumer(c,"one");
Consumer con2 = new Consumer(c,"two");
Consumer con3 = new Consumer(c,"three");}}

The above code is not working for single producer and multiple consumers
eventhough I have used notifyAll(). Whats the exact function of notifyAll.
Tell me what should I do if it has to work. or else suggest me some material on NET.

Where there are more wonders there is less knowledge
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

Format the code so it is readable. What you have hurts my eyes.

Ed's latest article: A Java Parallel Calamity
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078

I've formatted your code so it's easier to read. Please do this in the future and use the code tags so that the formatting sticks. Part of this would be not putting more than one line of code on a single line. This will make it much easier for us to read and understand the code (and help you) in the future. Thanks!
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Sagar kanchi:
... The above code is not working for single producer and multiple consumers eventhough I have used notifyAll(). Whats the exact function of notifyAll...

It depends on what you mean by "working." Is your goal for each consumer to get each item? (In other words, each consumer should end up with a "full set" of items.) Or do you simply want each item to go to a single consumer? (That is, the items will be divided up among the different consumers.)

In any case, adding the following as the first line of your get method should help you see what's happening...

System.out.println(Thread.currentThread().getName() + " calling get. isItemSet is " + isItemSet);

And checking the API for Object should help clarify what notifyAll does.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Henry Wong

Joined: Sep 28, 2004
Posts: 18876

First of all, change the "if" to a "while" (for both the get() and put() methods). Just because you get a notification does not mean that the condition is what you wanted -- particulary since you are using notify all.

I also generally preferred to use notify() instead of notifyAll() since technically you only need one thread to wake up, when the condition is set. Unfortunately, in this case, you must use notifyAll(), since you have both your producers and consumers waiting on the same condition variable, you can't control whether you wake up a producer or consumer.

[ October 19, 2005: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
GeeCON Prague 2014
subject: notifyAll()