Deamon thread Factory

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Deamon thread Factory

Kasper Nielsen-2
Hi,

I find myself using daemon threads quite often. However the utility
class Executors lacks methods for this. It would be nice with an easy
way to create daemon threads.

Perhaps a static ThreadFactory daemonThreadFactory(); method

- Kasper

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Deamon thread Factory

tpeierls
Daemon threads should be used sparingly, for reasons given in Sec 7.4.2 of JCiP. Adding such a method to Executors would inevitably encourage people to use daemon threads even where inappropriate. Better to have the folks who really need a daemonThreadFactory() method roll their own. It's easy enough:

public static ThreadFactory daemonThreadFactory() {
    final ThreadFactory f = Executors.defaultThreadFactory();
    return new ThreadFactory() {
        public Thread newThread(Runnable r) {
            Thread t = f.newThread(r);
            t.setDaemon(true);
            return t;
        }
    };
}

--tim

On 10/20/06, Kasper Nielsen <[hidden email]> wrote:
Hi,

I find myself using daemon threads quite often. However the utility
class Executors lacks methods for this. It would be nice with an easy
way to create daemon threads.

Perhaps a static ThreadFactory daemonThreadFactory(); method

- Kasper

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Deamon thread Factory

Moran Avigdor
This is a useful code snippet, especially since Executors.DefaultThreadFactory can't be extended.
I stumbled upon this issue long time ago, and ended up writing a
PriorityThreadFactory - out of
the DefaultThreadFactory implementation.

Seeing this, I now went back and deleted the
PriorityThreadFactory class and done as suggested
below - this is much less error prone.

Thanks.

Moran


Tim Peierls wrote:
Daemon threads should be used sparingly, for reasons given in Sec 7.4.2 of JCiP. Adding such a method to Executors would inevitably encourage people to use daemon threads even where inappropriate. Better to have the folks who really need a daemonThreadFactory() method roll their own. It's easy enough:

public static ThreadFactory daemonThreadFactory() {
    final ThreadFactory f = Executors.defaultThreadFactory();
    return new ThreadFactory() {
        public Thread newThread(Runnable r) {
            Thread t = f.newThread(r);
            t.setDaemon(true);
            return t;
        }
    };
}

--tim

On 10/20/06, Kasper Nielsen <[hidden email]> wrote:
Hi,

I find myself using daemon threads quite often. However the utility
class Executors lacks methods for this. It would be nice with an easy
way to create daemon threads.

Perhaps a static ThreadFactory daemonThreadFactory(); method

- Kasper

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________ Concurrency-interest mailing list [hidden email] http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest