How to run a method periodically (inside a servlet)?

Z

zaku

Hi,
I have to run a piece of code on a server periodically (I have to get
the server time and send it to the frontend application). However, my
problem is:

when my servlet starts I have to repeat the execution of a method,
let's say every 30 minutes.

Could somebody suggest me how could I run this piece of code inside the
servlet periodically?

Thank you very much for your answerz
John
 
K

Karsten Baumgarten

Hi,
I have to run a piece of code on a server periodically (I have to get
the server time and send it to the frontend application). However, my
problem is:

when my servlet starts I have to repeat the execution of a method,
let's say every 30 minutes.

Could somebody suggest me how could I run this piece of code inside the
servlet periodically?

Thank you very much for your answerz

Search for Timer and TimerTask in the Java API Reference.
 
S

Sanjay

Using Timer class will keep creating and terminating the threads, it
could be costly.
Why dont you just go ahead and create your own thread that wakes the
main thread up periodically and executes the method.
 
A

Antti S. Brax

Using Timer class will keep creating and terminating the threads, it
could be costly.

How exactly does a Timer create and terminate threads? All timer
tasks are executed by the same thread. That is why the API
documentation for Timer says that the tasks should not take long
to execute (because if they did they could postpone the execution
of other tasks).

And since the task is run once every 30 minutes I don't think any
thread creation could become a bottleneck.
Why dont you just go ahead and create your own thread that wakes the
main thread up periodically and executes the method.

Why don't you go back to inventing wheels. You seem to be good
at it.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top