Thread and ThreadFactory
I spent some time to read the implementation of Thread and ThreadFactory class. Because I’m confused about the choice between them. Then I found that I had little understanding of the Thread creation previously. As when we create a Thread with new Thread(xxx)
, it has done a lot of things for us.
Create Thread with Thread constructor
Below is the most basic constructor. It calls init method to do a lot of settings.
public Thread(ThreadGroup group, Runnable target, String name,
long stackSize) {
init(group, target, name, stackSize);
}
The init method helps set the ThreadGroup, Parent, Daemon and priority of thread. But what expose to user only is the control of Group. By default the ThreadGroup, Priority, daemon are inherit from current thread. So when we execute below code, we can see the ThreadGroup is main and Priority is Thread.NORM_PRIORITY inherited from main thread.
println(Thread.currentThread().getThreadGroup) //java.lang.ThreadGroup[name=main,maxpri=10]
println(Thread.currentThread().getPriority) // 5
val thread = new Thread("test")
println(thread.getThreadGroup) // java.lang.ThreadGroup[name=main,maxpri=10]
println(thread.getPriority) // 5
ThreadFactory
ThreadFactory is an interface which can be implemented to control the creation of thread. It can provide more grain control of the basic attributes of Thread like Priority, Group, daemon state. Any thread created by ThreadFactory has unified properties. Let’s see a DefaultThreadFactory from Executors class.
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
The universal control provided:
- universe naming convention: pool-poolNumber-thread-threadNumber
- non daemon thread
- priority is norm (5)
- one ThreadGroup
Conclusion
ThreadFactory provides more grain and universal control of thread creation. If you want to create series fo threads with universal naming convention or state and control them together with ThreadGroup, you can use ThreadFactory to implement your custom logic.