Valhalla Legends Forums Archive | Java Programming | thread mystery

AuthorMessageTime
freedom
i am very much confused with the wait-notify concept in thread tutorial.

say, i have created 2 threads like

th1.start();
th2.start();


question :  is this wait-notify will be called   if and only if they share the same variable  ?

question 2 : say,  how do i call wait-notify method here . will you please explain and simplify this mystery ?
August 23, 2005, 1:46 AM
freedom
nobody comes  here!
August 23, 2005, 9:32 AM
The-FooL
I've never used the wait-notify methods on threads ... I found it to confusing as well, and never really needed it.  But don't you have to call the wait/notify methods yourself?  What are you trying to do?
August 23, 2005, 12:16 PM
Myndfyr
If you want it to wait automatically, apply the [code]synchronized[/code] keyword to the methdods.  If you want it to always use the most recent value automatically, use the [code]volatile[/code] keyword on the variable.  See reference.
August 24, 2005, 12:32 AM
gameschild
wait and notify can be called on objects or classes.

eg
[code]


public class m {

public static void main(String[] args) {
m MA = new m();
}

public m() {
//create a thread
Thread1 t1 = new m.Thread1();
//create a thead with a ref to the other thread
Thread2 t2 = new Thread2(t1);

t1.start();
t2.start();
}

public class Thread1 extends Thread {
public void run() {
System.out.println("T1: waiting to be notified");
try {
//synchronized and wait
synchronized(this) {
this.wait();
}
}catch(Exception e) {
System.out.println("T1: Error - " + e);
}
System.out.println("T1: notified");
}
}

protected class Thread2 extends Thread {

private Thread1 otherThread;

public Thread2(Thread1 aThread) {
otherThread = aThread;
}

public void run() {
//synchronized and notified the other thread
synchronized(otherThread) {
System.out.println("T2: Notifing other thread");
otherThread.notify();
System.out.println("T2: Other Thread Notified");
}
}
}
}
[/code]

Instead of synchronizing on "this" you can use Objects as such:
[code]

public class m {

private Object sync ;

public static void main(String[] args) {
m MA = new m();
}

public m() {
//create an object
sync = new Object();

//create a thread with a ref to the obect
Thread1 t = new Thread1(sync);

synchronized(sync) {
//start the thread
t.start();
System.out.println("Main: telling Object to wait...");
try {
//tell the object to wait...
sync.wait();
}catch(Exception e) {
System.out.println("Main: Error - " + e);
}
System.out.println("Main: Object notified");
}
}

protected class Thread1 extends Thread {

private Object syncLock;

public Thread1(Object aSyncLock) {
syncLock = aSyncLock;
}

public void run() {
try {
System.out.println("Thread1: Waiting 5 seconds...");
//tell the thread to wait 500ms and then...
sleep(500);
}catch(Exception e) {
System.out.println("Error: " + e);
}
synchronized(syncLock) {
System.out.println("Thread1: Notifying Object...");
//...(and then) notify the object
syncLock.notify();
System.out.println("Thread1: Object Notified");
}
}

}
}
[/code]
September 26, 2005, 1:52 AM

Search