aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "notify()" Watch "notify()" New topic
Author

notify()

Alex Black
Greenhorn

Joined: Apr 28, 2001
Posts: 16
Hi all
I read somewhere that the notify() method must be called by another thread.
Thus, If anyone could enlighten me I was wondering why in this
piece of code at //1 I can't seem to get Counter A out of the
waiting state.
Thanks
Alex
------------------------------
class Counter extends Thread {
private int currentValue;

private static boolean flag;
public Counter(String threadName) {
super(threadName);
currentValue = 0;
System.out.println(this);
start();
}
public void run() {


while (currentValue < 5) {
System.out.println(getName() + ": " + (currentValue++));
getSynchronized();


}
System.out.println("Exit from " + getName() + ".");


}
public int getValue() { return currentValue; }

public synchronized void getSynchronized(){
System.out.println("I am synchonized!");
try{
if (!flag){
System.out.println("waiting");
flag = true;
wait();
}
else {
System.out.println("I didn't wait");
notify(); //1
}

}
catch(InterruptedException e){ System.out.println("Interrupted");}
}
}
public class TestClient {
public static void main(String args[]) {
Counter cA = new Counter("Counter A");
cA.setPriority(10);
Counter cB = new Counter("Counter B");
try {
System.out.println("Wait for the child threads to finish.");
cA.join();
cB.join();
if (!cA.isAlive())
System.out.println("Counter A not alive.");
if (!cB.isAlive())
System.out.println("Counter B not alive.");
} catch (InterruptedException e) {
System.out.println("Main Thread interrupted.");
}
System.out.println("Exit from Main Thread.");
}
}
[This message has been edited by Alexander Black (edited May 18, 2001).]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
A Thread that has a synchronized lock on the object where the other Thread is waiting must call notify()
Near as I can tell, your Threads are independent so they wait for ever.
Bill

------------------
author of:
Alex Black
Greenhorn

Joined: Apr 28, 2001
Posts: 16
Hi Bill,
Thanks so much for the help with threads. I was able to get over that hurdle with this piece of code:
class Object_Notifyer extends Object_User {
public Object_Notifyer(String threadName, Object_ aObject) {
super(threadName, aObject);
}
public void run() { object.method1notify();}
}
class Object_Waiter extends Object_User {
public Object_Waiter(String threadName, Object_ aObject) {
super(threadName, aObject);
}
public void run() { object.method2wait(new String("thread to waiter")); }
}
abstract class Object_User extends Thread {
protected Object_ object;
public Object_User(String threadName, Object_ aObject) {
super(threadName);
object = aObject;
System.out.println(this);
setDaemon(true);
start();
}
}

class Object_ {
private Object objectArray[];
public Object_ (int capacity) {
objectArray = new Object[capacity];
}

public synchronized Object method1notify() {
System.out.println(Thread.currentThread() + ": notifying");
Object obj = objectArray[0];
notify();
return obj;
}
public synchronized void method2wait(Object element) {
try {
System.out.println(Thread.currentThread() + ": waiting");
wait();
} catch (InterruptedException e) { }
System.out.println(element + ": not waiting");
objectArray[0] = element;

}
}
----------------------------
Anyway,just to see that someone such as yourself actually posts answers to questions such as mine is really inspiring.
Thanks and Regards,
Alex

 
Don't get me started about those stupid light bulbs.
 
subject: notify()