1 package com.github.triceo.splitlog.util;
2
3 import java.util.concurrent.ThreadFactory;
4 import java.util.concurrent.atomic.AtomicLong;
5
6 public class SplitlogThreadFactory implements ThreadFactory {
7
8 private static final AtomicLong UNIQUE_ID = new AtomicLong(0);
9 private final AtomicLong nextId = new AtomicLong(0);
10
11 private final ThreadGroup threadGroup;
12 private final String threadGroupName;
13
14 public SplitlogThreadFactory(final String threadGroupName) {
15 this.threadGroupName = threadGroupName;
16 this.threadGroup = new ThreadGroup(threadGroupName);
17 }
18
19 @Override
20 public Thread newThread(final Runnable r) {
21 final Thread t = new Thread(this.threadGroup, r, this.newThreadName());
22 t.setDaemon(true);
23 return t;
24 }
25
26 private String newThreadName() {
27 return this.threadGroupName + "-" + this.nextId.incrementAndGet() + "-"
28 + SplitlogThreadFactory.UNIQUE_ID.incrementAndGet();
29 }
30
31 }