Valhalla Legends Archive

Programming => General Programming => Java Programming => Topic started by: freedom on August 22, 2005, 08:46 PM

Title: thread mystery
Post by: freedom on August 22, 2005, 08:46 PM
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 ?
Title: Re: thread mystery
Post by: freedom on August 23, 2005, 04:32 AM
nobody comes  here!
Title: Re: thread mystery
Post by: The-FooL on August 23, 2005, 07:16 AM
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?
Title: Re: thread mystery
Post by: MyndFyre on August 23, 2005, 07:32 PM
If you want it to wait automatically, apply the synchronized keyword to the methdods.  If you want it to always use the most recent value automatically, use the volatile keyword on the variable.  See reference (http://www-128.ibm.com/developerworks/java/library/j-threads1.html).
Title: Re: thread mystery
Post by: gameschild on September 25, 2005, 08:52 PM
wait and notify can be called on objects or classes.

eg



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");
}
}
}
}


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


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");
}
}

}
}