thread running or waiting?

R

rick bryant

anyone:

here's the scenario: two threads (workerThread and monitorThread) are
running and workerThread acquires a lock on workerLock object.
monitorThread is monitoring the state of workerThread. is there any
way for monitorThread to find out if workerThread has a lock on
workerLock object? i ultimately want monitorThread to be able to
determine whether workerThread is in a running or waiting state.

quick code sample of what i'm trying to accomplish:

public class ProcessBase {
private volatile Object workerLock = new Object();
private volatile Thread workerThread;

public ProcessBase() {
startMonitorThread();
startWorkerThread();
}

private void startMonitorThread() {
final Object monitorLock = new Object();
Runnable runnable = new Runnable() {
public void run() {
while( true ) {
// at this point in the code,
// want to find out if workerThread is running or
waiting
try {
monitorLock.wait( 2000 );
}
catch( InterruptedException ie ) {
ie.printStackTrace();
}
}
}
};
Thread monitorThread = new Thread( runnable );
monitorThread.start();
}

private void startWorkerThread() {
Runnable runnable = new Runnable() {
public void run() {
while( true ) {
try {
synchronized( workerLock ) {
doWork();
workerLock.wait( 10000 );
}
}
catch( InterruptedException ie ) {
ie.printStackTrace();
}
}
}
};
workerThread = new Thread( runnable );
workerThread.start();
}

private void doWork() {
// insert code for work
}

}

thanks so much!
rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top