blob: 38c5bafc0d6beb453fa0c0b879a808359bb97dcb (
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
|
// Copyright 2016 Yahoo Inc. 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 <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
*/
public class DaemonThreadFactory implements ThreadFactory {
private 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;
}
}
|