Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

notifyAll()

 
Sagar kanchi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Format the code so it is readable. What you have hurts my eyes.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20999
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic