[lang] Add a DaemonThreadFactory

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

[lang] Add a DaemonThreadFactory

garydgregory
I keep on copying this from project to project and it seems fitting for
Commons Lang:

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

public final class DaemonThreadFactory implements ThreadFactory {

    private final static AtomicLong COUNT = new AtomicLong(1);

    private final String name;

    public DaemonThreadFactory(final String name) {
        super();
        this.name = name;
    }

    @Override
    public Thread newThread(final Runnable r) {
        final Thread thread = new Thread(r, name + " " +
COUNT.getAndIncrement());
        thread.setDaemon(true);
        return thread;
    }

}

Thoughts?

Gary
Reply | Threaded
Open this post in threaded view
|

Re: [lang] Add a DaemonThreadFactory

Oliver Heger-3


Am 19.12.2017 um 17:27 schrieb Gary Gregory:
> I keep on copying this from project to project and it seems fitting for
> Commons Lang:

In the concurrent package, there is the BasicThreadFactory class. Could
this class satisfy your needs or can it be extended?

Oliver

>
> import java.util.concurrent.ThreadFactory;
> import java.util.concurrent.atomic.AtomicLong;
>
> public final class DaemonThreadFactory implements ThreadFactory {
>
>     private final static AtomicLong COUNT = new AtomicLong(1);
>
>     private final String name;
>
>     public DaemonThreadFactory(final String name) {
>         super();
>         this.name = name;
>     }
>
>     @Override
>     public Thread newThread(final Runnable r) {
>         final Thread thread = new Thread(r, name + " " +
> COUNT.getAndIncrement());
>         thread.setDaemon(true);
>         return thread;
>     }
>
> }
>
> Thoughts?
>
> Gary
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [lang] Add a DaemonThreadFactory

Roger Whitcomb
In reply to this post by garydgregory
I have a class almost identical to this in my project, so +1 to this idea.

~Roger Whitcomb

-----Original Message-----
From: Gary Gregory [mailto:[hidden email]]
Sent: Tuesday, December 19, 2017 8:27 AM
To: Commons Developers List <[hidden email]>
Subject: [lang] Add a DaemonThreadFactory

I keep on copying this from project to project and it seems fitting for Commons Lang:

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

public final class DaemonThreadFactory implements ThreadFactory {

    private final static AtomicLong COUNT = new AtomicLong(1);

    private final String name;

    public DaemonThreadFactory(final String name) {
        super();
        this.name = name;
    }

    @Override
    public Thread newThread(final Runnable r) {
        final Thread thread = new Thread(r, name + " " + COUNT.getAndIncrement());
        thread.setDaemon(true);
        return thread;
    }

}

Thoughts?

Gary

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: [lang] Add a DaemonThreadFactory

Charles Honton
I also have one of these.

+1

Chas

> On Dec 21, 2017, at 7:34 AM, Roger Whitcomb <[hidden email]> wrote:
>
> I have a class almost identical to this in my project, so +1 to this idea.
>
> ~Roger Whitcomb
>
> -----Original Message-----
> From: Gary Gregory [mailto:[hidden email]]
> Sent: Tuesday, December 19, 2017 8:27 AM
> To: Commons Developers List <[hidden email]>
> Subject: [lang] Add a DaemonThreadFactory
>
> I keep on copying this from project to project and it seems fitting for Commons Lang:
>
> import java.util.concurrent.ThreadFactory;
> import java.util.concurrent.atomic.AtomicLong;
>
> public final class DaemonThreadFactory implements ThreadFactory {
>
>    private final static AtomicLong COUNT = new AtomicLong(1);
>
>    private final String name;
>
>    public DaemonThreadFactory(final String name) {
>        super();
>        this.name = name;
>    }
>
>    @Override
>    public Thread newThread(final Runnable r) {
>        final Thread thread = new Thread(r, name + " " + COUNT.getAndIncrement());
>        thread.setDaemon(true);
>        return thread;
>    }
>
> }
>
> Thoughts?
>
> Gary
> B‹KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKCB•È[œÝXœØÜšX™KK[XZ[ˆ]‹][œÝXœØÜšX™PÛÛ[[ۜ˘\XÚK›Ü™ÃB‘›ÜˆY][Û˜[ÛÛ[X[™ËK[XZ[ˆ]‹Z[ÛÛ[[ۜ˘\XÚK›Ü™ÃB

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [lang] Add a DaemonThreadFactory

garydgregory
In reply to this post by Oliver Heger-3
Thank you Oliver. I replaced my code with:

import java.util.concurrent.ThreadFactory;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

public final class DaemonThreadFactory {

    public static ThreadFactory create(String namingPattern) {
        // @formatter:off
        return new BasicThreadFactory.Builder()
            .namingPattern(namingPattern)
            .daemon(true)
            .priority(Thread.MAX_PRIORITY)
            .build();
        // @formatter:on
    }

}
Gary

On Tue, Dec 19, 2017 at 1:34 PM, Oliver Heger <[hidden email]>
wrote:

>
>
> Am 19.12.2017 um 17:27 schrieb Gary Gregory:
> > I keep on copying this from project to project and it seems fitting for
> > Commons Lang:
>
> In the concurrent package, there is the BasicThreadFactory class. Could
> this class satisfy your needs or can it be extended?
>
> Oliver
>
> >
> > import java.util.concurrent.ThreadFactory;
> > import java.util.concurrent.atomic.AtomicLong;
> >
> > public final class DaemonThreadFactory implements ThreadFactory {
> >
> >     private final static AtomicLong COUNT = new AtomicLong(1);
> >
> >     private final String name;
> >
> >     public DaemonThreadFactory(final String name) {
> >         super();
> >         this.name = name;
> >     }
> >
> >     @Override
> >     public Thread newThread(final Runnable r) {
> >         final Thread thread = new Thread(r, name + " " +
> > COUNT.getAndIncrement());
> >         thread.setDaemon(true);
> >         return thread;
> >     }
> >
> > }
> >
> > Thoughts?
> >
> > Gary
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>