aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/concurrent/DaemonThreadFactory.java
blob: c1a3ee30b9ac6c0529c0e363afba28f2a6ea59a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.concurrent;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * A simple thread factory that decorates <code>Executors.defaultThreadFactory()</code>
 * and sets all created threads to be daemon threads.
 *
 * @author Einar M R Rosenvinge
 */
public class DaemonThreadFactory implements ThreadFactory {

    private final ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
    private String prefix = null;

    /**
     * Creates a deamon thread factory that creates threads with the default names
     * provided by <code>Executors.defaultThreadFactory()</code>.
     */
    public DaemonThreadFactory() {
    }

    /**
     * Creates a deamon thread factory that creates threads with the default names
     * provided by <code>Executors.defaultThreadFactory()</code> prepended by the
     * specified prefix.
     *
     * @param prefix the thread name prefix to use
     */
    public DaemonThreadFactory(String prefix) {
        this.prefix = prefix;
    }

    public String getPrefix() {
        return prefix;
    }

    @Override
    public Thread newThread(Runnable runnable) {
        Thread t = defaultThreadFactory.newThread(runnable);
        t.setDaemon(true);
        if (prefix != null) {
            t.setName(prefix + t.getName());
        }
        return t;
    }

}