R
Razvan
Hi !
When the object is released from a lock (at the end of the
synchronized block) a notify message gets sent by default ? The
following piece of code seems to suggest that:
public class CThread extends Thread
{
public static void main(String args[])
{
// create & start the FIRST thread
CThread th1 = new CThread();
System.out.println("\nStarting thread 1...\n");
th1.start();
// sleep for a second
try { Thread.sleep(1000); }
catch(InterruptedException ee) { System.out.println("The main thread
was interrupted !"); }
// create & start the SECOND thread
Runner rn = new Runner(th1);
Thread th2 = new Thread(rn);
System.out.println("\nStarting thread 2...\n");
th2.start();
}
public synchronized void run()
{
int ii = 3;
while(ii > 0)
{
System.out.println("\t\t\tthread 1: \t\t\tii=" + ii);
notify();
try { wait(); }
catch(InterruptedException ee) { System.out.println("Thread 1
interrupted from wait [1]!"); }
ii--;
}
System.out.println("\nThread 1 is waiting for 5 seconds !");
try {
// notify(); // if I notify here the second thread will finish
first
// if I don't notify the first one will finish first
// this seems to point to the fact that at the end of
// the synchronized method run() some notify() message is sent
wait(5000);
}
catch(InterruptedException ee) { System.out.println("Thread 1
interrupted from wait [2]!"); }
System.out.println("Thread 1 has finished !");
}
}
class Runner implements Runnable
{
CThread externalThreadObject;
// Runner constructor
Runner(CThread externalThreadObject) { this.externalThreadObject =
externalThreadObject; }
public void run()
{
int ii = 3;
while(ii > 0)
{
System.out.println("\t\t\tthread 2: \t\t\tii=" + ii);
synchronized(externalThreadObject)
{
externalThreadObject.notify();
try {
externalThreadObject.wait();
}
catch(InterruptedException ee) {
System.out.println("Second thread interrupted from wait !");
}
}
ii--;
}
System.out.println("Thread 2 has finished !");
}
}
Normally the second thread should hang indefinitively in the wait()
call (the wait call has no timeout) but it is not. As soon as the
first thread finishes the synchronized block, the second thread
receives a notify() message from somewhere and the wait() call ends.
Why is this happening ?
Regards,
Razvan
When the object is released from a lock (at the end of the
synchronized block) a notify message gets sent by default ? The
following piece of code seems to suggest that:
public class CThread extends Thread
{
public static void main(String args[])
{
// create & start the FIRST thread
CThread th1 = new CThread();
System.out.println("\nStarting thread 1...\n");
th1.start();
// sleep for a second
try { Thread.sleep(1000); }
catch(InterruptedException ee) { System.out.println("The main thread
was interrupted !"); }
// create & start the SECOND thread
Runner rn = new Runner(th1);
Thread th2 = new Thread(rn);
System.out.println("\nStarting thread 2...\n");
th2.start();
}
public synchronized void run()
{
int ii = 3;
while(ii > 0)
{
System.out.println("\t\t\tthread 1: \t\t\tii=" + ii);
notify();
try { wait(); }
catch(InterruptedException ee) { System.out.println("Thread 1
interrupted from wait [1]!"); }
ii--;
}
System.out.println("\nThread 1 is waiting for 5 seconds !");
try {
// notify(); // if I notify here the second thread will finish
first
// if I don't notify the first one will finish first
// this seems to point to the fact that at the end of
// the synchronized method run() some notify() message is sent
wait(5000);
}
catch(InterruptedException ee) { System.out.println("Thread 1
interrupted from wait [2]!"); }
System.out.println("Thread 1 has finished !");
}
}
class Runner implements Runnable
{
CThread externalThreadObject;
// Runner constructor
Runner(CThread externalThreadObject) { this.externalThreadObject =
externalThreadObject; }
public void run()
{
int ii = 3;
while(ii > 0)
{
System.out.println("\t\t\tthread 2: \t\t\tii=" + ii);
synchronized(externalThreadObject)
{
externalThreadObject.notify();
try {
externalThreadObject.wait();
}
catch(InterruptedException ee) {
System.out.println("Second thread interrupted from wait !");
}
}
ii--;
}
System.out.println("Thread 2 has finished !");
}
}
Normally the second thread should hang indefinitively in the wait()
call (the wait call has no timeout) but it is not. As soon as the
first thread finishes the synchronized block, the second thread
receives a notify() message from somewhere and the wait() call ends.
Why is this happening ?
Regards,
Razvan