Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One thread as a scheduler on webserver

 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a thread which automatically start up when the application is loaded (load-on-startup).

The thread is querying the database, to see if some jobs have to be startet. The problem is that when I change in the code under development without restarting the web server, a new thread is started. Then I have more threads running and that cause me problems, when the threads write to the database, even if I had all my methods synchronized. How do I ensure that only one thread is allowed to run?

When I stop or restart the web server, I want the single running thread to be killed (removed). How do I ensure that the thread is killed when the web server is stopped or restarted?

How can I use the setDaemon method. I don�t understand the concept?
[ September 10, 2005: Message edited by: Jeppe Fjord ]
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of the structure of the code. The thread is running in the servlet. I�m not extending the Thread class and the Runnable interface is not implemented.

Is there any problems about that?

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you switch to using the util Timer? It manages the thread and you manage the tasks that are run on schedule. Keep the timer reference in a stable class that doesn't change, and make the class that changes remove all currently scheduled tasks and schedule new tasks.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I think that�s a better way to do it. I didn�t knew about the TimerTask.

What about the parameter isDaemon ( new Timer( boolean isDaemon )).

How can you use that?

[ September 11, 2005: Message edited by: Jeppe Fjord ]
[ September 12, 2005: Message edited by: Jeppe Fjord ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you end your main thread and want to stop the program, the JVM will simply terminate any daemon threads. If there are any non-daemon threads it will wait for them to end on their own. I ran into trouble with non-daemon threads that never ended, so the JVM continued to run and show up in task manager when I thought the program was shut down.

I think shutting down with Runtime.exit() will kill daemons and non-daemons just fine. Try it and see!
 
Henry Wong
author
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure you should be starting threads in a servlet, but if you insist...

Start the thread, just like you did, in the init() method. But have the thread monitor some kind of done flag. If the done flag is set, then this thread should stop what it is doing and exit.

Then from the servlet, have the destroy() method set the flag.

Henry
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure you should be starting threads in a servlet, but if you insist...


Yup, that seems wrong in some way, doesn't it? I don't think the servlet spec gives you any other way to start something at application startup, though, so this is a fairly common "misuse" of the "initialize this servlet at startup" configuration.

There are also cases where we start a new thread at user request, return a "process started" page and do more work in background. It's challenging to get the results of the worker thread back to the user, but do-able.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic