Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

servlet threads 1

Status
Not open for further replies.

zapster

Programmer
Jun 8, 2001
36
GB
Hi

I have a servlet which I would like to carry out db maintenance. I can do db maintenance side but I would like the methods to run at x time, ie once a week. or once a day

I found a servlet from which generates a thread to carry out the timing process but. I would like to stop the thread, and restart the thread. I have read that there can be deadlock issues with doing this. What i don't want to do is create new instances of the timer class, which I originaly setup in the init.

Does anybody know how I could get round this or have a servlet that can do this, ie can stop and start the maintenace calls (that are running x days) through a web controller page

Thanks

Kenny
 
Hi Kenny,

I suggest that you can use javascript as the timer instead? What will happen is that you can edit the codes within the javascript to run the servlet at anytime you want. This way, it will reduce a lot of memory usage as creating a java program to keep on checking the date and time could be quite taxing on memory.

Another good point in using javascript is you can create your own buttons easily to restart or stop the timer since all you have to do is to just set the "countdown" time to the original value or pause the "countdown" temporarily.

However a bad point is you must keep the browser always opened...

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Hi Leon,

The javascript idea sound good, but as you have pointed out I would have to keep my browser window open. :(

I realy need to control all this by a servlet.

Any ideas please

thanks again

Kenny
 
Seriously speaking, I don't know of any other method...

I never tried doing this before but this is what I guess will happen. Lets say I call this thread-servlet of mine using a browser. Since the servlet must always be running, so when the time reaches, it will do some maintenance of your db.

This might hang the browser since the servlet will run non-stop. The same thing will happen if I call the servlet using other apps. So if your browser hangs, I guess it wouldn't be any better if you have a javascript as a Timer instead.

Of course this is only what I guess will happen. It might not hang the browser, who knows until someone has tried it out?

Hope this helps,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
hi kenny,
once you stop a thread, you can not restart it. it is in adead state. if you want this thread functionality again, you must create a new thread. having said that, you can put your thread i a sleeping state by calling

Thread.sleep();

I assume your timer thread is doing this already but it is specifing a a time limit to the sleep i.e.

Thread.sleep( 10000 );

What i would do it this. create to accessor methods. 1 called resume, the other called pause like such

/**
* Notified all threads that are in a waiting state to resume
*/
public void resume()
{
Thread.notifyAll();
}

/**
* Tell this thread to wait until called again
*/
public void pause()
{
Thread.wait();
}

what wait() does is to tell the thread to stop executing. NofigyAll() tells all threads in a waiting state to 'wake up' and continue executing

best of luck :)
 
Well, I always added a function as the following to my Thread classes (with a boolean instance variable called stopPlease):

public void stopPlease()
{
this.stopPlease = true;
}

The run() function would then look as follows:

public void run()
{
this.stopPlease = false;
while (!this.stopPlease)
{
// do stuff
}
}

You can then call stopPlease() and start() as many times as you want...

Hope this helps
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
this also works however, after you call stopPlease(), the thread then moves into a dead state. it can not be restarted, so a new thread needs to be kicked off.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top