1 /* 2 * Hunt - A refined core library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.concurrency.ThreadFactory; 13 14 import hunt.concurrency.atomic.AtomicHelper; 15 import hunt.concurrency.thread.ThreadEx; 16 17 import hunt.Functions; 18 import hunt.util.Common; 19 20 import core.thread; 21 import std.conv; 22 23 /** 24 * An object that creates new threads on demand. Using thread factories 25 * removes hardwiring of calls to {@link Thread#Thread(Runnable) new Thread}, 26 * enabling applications to use special thread subclasses, priorities, etc. 27 * 28 * <p> 29 * The simplest implementation of this interface is just: 30 * <pre> {@code 31 * class SimpleThreadFactory implements ThreadFactory { 32 * public Thread newThread(Runnable r) { 33 * return new Thread(r); 34 * } 35 * }}</pre> 36 * 37 * The {@link Executors#defaultThreadFactory} method provides a more 38 * useful simple implementation, that sets the created thread context 39 * to known values before returning it. 40 * @author Doug Lea 41 */ 42 interface ThreadFactory { 43 44 /** 45 * Returns a default thread factory used to create new threads. 46 * This factory creates all new threads used by an Executor in the 47 * same {@link ThreadGroupEx}. If there is a {@link 48 * java.lang.SecurityManager}, it uses the group of {@link 49 * System#getSecurityManager}, else the group of the thread 50 * invoking this {@code defaultThreadFactory} method. Each new 51 * thread is created as a non-daemon thread with priority set to 52 * the smaller of {@code Thread.PRIORITY_DEFAULT} and the maximum 53 * priority permitted in the thread group. New threads have names 54 * accessible via {@link Thread#getName} of 55 * <em>pool-N-thread-M</em>, where <em>N</em> is the sequence 56 * number of this factory, and <em>M</em> is the sequence number 57 * of the thread created by this factory. 58 * @return a thread factory 59 */ 60 static ThreadFactory defaultThreadFactory() { 61 return new DefaultThreadFactory(); 62 } 63 64 /** 65 * Constructs a new {@code Thread}. Implementations may also initialize 66 * priority, name, daemon status, {@code ThreadGroupEx}, etc. 67 * 68 * @param r a runnable to be executed by new thread instance 69 * @return constructed thread, or {@code null} if the request to 70 * create a thread is rejected 71 */ 72 Thread newThread(Runnable r); 73 74 // final Thread newThread(Action dg ) { 75 76 // } 77 } 78 79 80 /** 81 * The default thread factory. 82 */ 83 private class DefaultThreadFactory : ThreadFactory { 84 private static shared(int) poolNumber = 1; 85 private ThreadGroupEx group; 86 private shared(int) threadNumber = 1; 87 private string namePrefix; 88 89 this() { 90 // SecurityManager s = System.getSecurityManager(); 91 // group = (s !is null) ? s.getThreadGroup() : 92 // Thread.getThis().getThreadGroup(); 93 int n = AtomicHelper.getAndIncrement(poolNumber); 94 namePrefix = "pool-" ~ n.to!string() ~ "-thread-"; 95 } 96 97 98 ThreadEx newThread(Runnable runnable ) { 99 int n = AtomicHelper.getAndIncrement(threadNumber); 100 101 ThreadEx t = new ThreadEx(runnable, namePrefix ~ n.to!string()); 102 t.isDaemon = false; 103 // version(Posix) { 104 // t.priority = Thread.PRIORITY_DEFAULT; 105 // } 106 107 return t; 108 } 109 } 110