Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > thread running or waiting?

Reply
Thread Tools

thread running or waiting?

 
 
rick bryant
Guest
Posts: n/a
 
      06-04-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
boost.thread library and the thread is running Miroslaw Makowiecki C++ 1 08-09-2007 07:14 PM
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
Thread was being aborted in win2003 server. Back ground thread reading MS access database, no redirects or transfers. Johanna ASP .Net 0 10-13-2004 01:32 PM
"Thread was being aborted" error from WebApp using Thread.Sleep. Stephen Miller ASP .Net 3 07-01-2004 11:50 PM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM



Advertisments