Sleeping 'watcher' servlet crashes JRUN

I have set up a servlet that runs a query once every 2 hours. I have made a while(true) loop that loops forever then executes the query then tells my thread that i created to sleep for 7200000 milliseconds. I tried using

Thread.currentThread.sleep(7200000) ;

and then also tried creating a new thread

Thread myThread = new Thread();

myThread.sleep(7200000);

(BTW: This is all in a method i call from the init method)

Either way if I invoke the servlet from a jsp it works fine but the jsp is always stuck working on getting a response. If I go into JRUN and set up the servlet to be preloaded when JRUN is started, it executes once then JRUN locks up. And when I try to view the site I see the "Could not connect to JRun Server" error. From this point on I have to complete hose the entire jrun instance and create a new jrun server instance. The JRUN instance cant be restarted, even rebooting doesnt work.

Is the servlet sleeping the jrun init thread itself? Im sorry Im new to threads.

Thanks for any help anyone can give me!

[1083 byte] By [cdalebikera] at [2007-9-27 23:46:44]
# 1

> Is the servlet sleeping the jrun init thread itself?

> Im sorry Im new to threads.

Yes. This is exactly what is happening.

You need to spawn another thread, that does nothing but runs through your while ( true ) ... loop, sleeping for the required two hours every now and then.

Something like this:

class MySleepingThread implements Runnable

{

public void run()

{

while ( true )

{

doTheStuffINeedToDoEveryTwoHours();

try

{

Thread.sleep( 1000 * 60 * 60 * 2 }

}

catch ( InterruptedException e )

{

break;

}

}

}

And in the servlet class you normally had this, do this:

Thread t = new Thread( new MySleepingThread() );

// This starts the thread and eventually calls the run() method, starting your loop on a separate thread, so this one will continue.

t.start();

.P.

praisaa at 2007-7-7 16:32:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Sorry, forgot a few curly braces, but you should be able to figure it out..P.
praisaa at 2007-7-7 16:32:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...