question on threads?

Hi,

I have an Object that gets locked by various threads via a synchronized block. Something Like:

class MyThread extends Thread{

public void run(){

synchronized(someObject){

// do stuff to some Object

}

}

}

Is there an easy way to have an another instance of MyThread examine "someObject" and see if it is locked by another thread? This way I could tell the user something like "waiting for locked resource..."

Thanks

[499 byte] By [yeonh] at [2007-9-26 9:23:40]
# 1

Hi,

Try with the following code, it stars two threads

which share the same object "MyThread" as shown below :

code:

class MyThread extends Thread {

boolean wait = false ;

public void run(){

callMe();

}

public synchronized void callMe() {

while( wait == true )

{

System.out.println(" Waiting for releasing the lock");

try{

wait();

}

catch( InterruptedException e ) { }

} System.out.println(" Locked ");

wait = true;

notifyAll();

}

public static void main( String[] args ) {

MyThread t = new MyThread();

new Thread(t).start();

new Thread(t).start(); }}

Hope this will help you

Anil.

Developer Technical Support

Sun Microsystems Inc,

http://www.sun.com/developers/support

ramanil_indts at 2007-7-1 20:42:37 > top of Java-index,Java HotSpot Virtual Machine,Specifications...