I think, it's because the object you're waiting on (obj.wait()) is not a Thread object but just a MyThread object.
I modified your code as following (to make it wait on a thread), and It will behave exactly as your first example (ie if you uncomment the notifyAll, the thread will resume execution)
class MyThread implements Runnable {
public void run() {
try {
synchronized (this) {
//System.out.println("Lock Released");
System.out.println("Lock Attained1 on " + this.toString());
// notifyAll(); //line #1
System.out.println("Lock Released1 on " + this.toString() );
}System.out.println("This Thread has completed" );
}catch(Exception e)
{e.printStackTrace();
}
}
public
String toString()
{return "MyThread";
}
}
class RunThis implements Runnable
{
MyThread obj = null;
Thread tt = null; public void run()
{
try
{
// synchronized(obj)
synchronized(tt) {
System.out.println("Lock Attained on " + obj.toString());
//obj.wait();
tt.wait(); System.out.println("Lock Released on "+obj.toString());
}}
catch(Exception e)
{e.printStackTrace();
}
}
public String toString()
{
return "RunThis";
}
public static void main(String[] args)
{
MyThread obj = new MyThread();
RunThis obj1 = new RunThis();
obj1.obj = obj;
Thread t = new Thread(obj);
obj1.tt = t; Thread t1 = new Thread(obj1);
t1.start();
t.start();
}
}
[ May 06, 2008: Message edited by: Mamadou Tour� ]
[ May 06, 2008: Message edited by: Mamadou Tour� ]