When should a thread be daemon?

R

Richard

Hello,

It was always my understanding that if you did not want a thread to
keep a program open then is should be a daemon thread. In other words
it is a background thread. I was doing some work with the
ScheduledExecutorService (http://java.sun.com/j2se/1.5.0/docs/api/java/
util/concurrent/ScheduledExecutorService.html) last week and I
suddenly realised that the thread was keeping my program open
occasionally. Digging a little and I discovered the thread that this
produces are not daemon. This goes against my understanding. The fact
that it is scheduled implies to me that it is doing a background task.
For this thread you must explicitly call shutdown on the executor in
order for it to die.

I have made my ScheduledExecutorService daemon by passing in my own
ThreadFactory and making sure that all the threads this produce are
daemon.

Is my understanding of what a daemon thread is correct? Is it odd to
anyone else that the ScheduledExecutorService produces non-daemon
thread?

Thanks
Richard
 
A

Andreas Leitgeb

Richard said:
Is my understanding of what a daemon thread is correct? Is it odd to
anyone else that the ScheduledExecutorService produces non-daemon
thread?

I think, whether a thread is scheduled or not is entirely orthogonal
to whether it is supposed to keep the application running or not.

If your scheduled threads are meant to be daemons, then making them
daemons the way you did seems appropriate to me.

Obviously, the designers of ScheduledExecutorService (or rather, the
designers of the actually used implementation class) had a different
feeling about the typical usecases than you. :)
 

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

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top