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
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