• Welcome to Valhalla Legends Archive.
 

thread mystery

Started by freedom, August 22, 2005, 08:46 PM

Previous topic - Next topic

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 ?

freedom


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?

MyndFyre

#3
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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

gameschild

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

}
}