Thread Code...anything wrong with this, must use java 1.4

Posted by bmw0128 on Stack Overflow See other posts from Stack Overflow or by bmw0128
Published on 2011-01-06T21:58:18Z Indexed on 2011/01/06 22:54 UTC
Read the original article Hit count: 176

Filed under:
|

I have a servlet automatically firing up when the app server starts, and in its init(), I'm making another thread:

init(){ new FooThread() }

in FooThread(), i want to periodically check the status of a DB value, then depending on the value, make a web service call. When these two tasks complete, I want the thread to sleep to wait a certain period then repeat. This cycle would just continue forever.

FooThread:

public class FooThread implements Runnable{
  Thread t;

  FooThread(){
    t = new Thread(this, "BBSThread");
    logger.info("*** about to start " + t.getName());
    t.start();
    logger.info("*** started: " + t);
  }

  public void run() {
    try{
      while(true){
        //do the db check, then conditionally do the web services call
        logger.info("*** calling sleep() ***");
        Thread.sleep(50000);
        logger.info("*** now awake ***");
      }
    } catch (InterruptedException e) {
      System.out.println("*** FooThread interrupted");
    }
  }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading