my dog learned polymorphism*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "notifyAll()" Watch "notifyAll()" New topic
Author

notifyAll()

Sagar kanchi
Ranch Hand

Joined: Oct 07, 2005
Posts: 50
The code is as follows

CubbyHole.java
================
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();}}
Producer.java
=============
class Producer extends Thread
{CubbyHole c;
Producer(CubbyHole c){
this.c = c;
start();}
public void run(){
int i=0;
while(true){
c.put(i++);}}}
Consumer.java
===============
class Consumer extends Thread{
CubbyHole c;
String name;
Consumer(CubbyHole c,String name){
this.c = c;
setName(name);
start();}
public void run(){
while(true){
c.get();}}}
Test.java
==========
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 http://coopsoft.com/ar/Calamity2Article.html
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
Sheriff

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
sscce.org
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40



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.

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: notifyAll()
 
Similar Threads
How to request monitor info on/from a locked object
producer/consumer
Producer Consumer question
threading access
Important, about threads