• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

notify()

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Beware the other head of science - it bites! Nibble on this message:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic