thread running or waiting?

Discussion in 'Java' started by rick bryant, Jun 4, 2004.

  1. rick bryant

    rick bryant Guest

    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
     
    rick bryant, Jun 4, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lonelyplanet999
    Replies:
    6
    Views:
    3,425
    John C. Bollinger
    Nov 18, 2003
  2. Replies:
    1
    Views:
    14,183
    Matt Humphrey
    Apr 22, 2005
  3. Simon Andrews

    Waiting (nicely!) for a thread

    Simon Andrews, Aug 15, 2005, in forum: Java
    Replies:
    7
    Views:
    727
    Thomas G. Marshall
    Aug 16, 2005
  4. Douwe
    Replies:
    3
    Views:
    8,492
    Randy Howard
    Aug 23, 2003
  5. Mr Dyl
    Replies:
    3
    Views:
    468
    Mr Dyl
    Jan 11, 2006
Loading...

Share This Page