当前位置:网站首页>Thread factory in thread pool
Thread factory in thread pool
2022-07-07 18:48:00 【Gravel under Mount Everest】
Since we all use thread pools , Then the work of creating threads is handed over to the thread pool . We just need to tell how many threads the thread pool needs , The rest of the work is left to the thread pool . Thread factories are used to create threads in the thread pool ,
Let's take a look at the thread factories :
ThreadFactory( Parent interface )
public interface ThreadFactory {
// Construct a new Thread . Implementations can also initialize priorities 、 name 、 Daemon status 、 ThreadGroup etc.
Thread newThread(Runnable r);
}
DefaultThreadFactory( By default )
// Default thread factory
static class DefaultThreadFactory implements ThreadFactory {
// Thread pool number
private static final AtomicInteger poolNumber = new AtomicInteger(1);
// Thread group
private final ThreadGroup group;
// Thread number
private final AtomicInteger threadNumber = new AtomicInteger(1);
// Thread name prefix
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
// Get thread groups
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
// Thread name prefix = pool + Thread pool number + thread
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
// Create a new thread
public Thread newThread(Runnable r) {
// Create thread ( Thread group , Mission , Thread name )
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
// Determine whether the current query is a daemon
// If it is a daemon thread, set it as a non daemon thread
if (t.isDaemon())
t.setDaemon(false);
// Determine whether the priority of the thread is the default priority
if (t.getPriority() != Thread.NORM_PRIORITY)
// Set the thread priority to the default priority
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
PrivilegedThreadFactory
// Privileged thread factory
// Thread factories capture access control contexts and class loaders
static class PrivilegedThreadFactory extends DefaultThreadFactory {
private final AccessControlContext acc;
private final ClassLoader ccl;
PrivilegedThreadFactory() {
super();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// Calls to getContextClassLoader from this class
// never trigger a security check, but we check
// whether our callers have this permission anyways.
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
// Fail fast
sm.checkPermission(new RuntimePermission("setContextClassLoader"));
}
this.acc = AccessController.getContext();
this.ccl = Thread.currentThread().getContextClassLoader();
}
public Thread newThread(final Runnable r) {
return super.newThread(new Runnable() {
public void run() {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
Thread.currentThread().setContextClassLoader(ccl);
r.run();
return null;
}
}, acc);
}
});
}
}
边栏推荐
- [sword finger offer] 59 - I. maximum value of sliding window
- Tips for this week 140: constants: safety idioms
- 【软件测试】从企业版BOSS直聘,看求职简历,你没被面上是有原因的
- [trusted computing] Lesson 11: TPM password resource management (III) NV index and PCR
- 你真的理解粘包与半包吗?3分钟搞懂它
- 不能忽略的现货白银短线操作小技巧
- Redis的发布与订阅
- Yearning-SQL审核平台
- 4种常见的缓存模式,你都知道吗?
- Hutool - 轻量级 DB 操作解决方案
猜你喜欢
Discuss | frankly, why is it difficult to implement industrial AR applications?
小程序中实现付款功能
用存储过程、定时器、触发器来解决数据分析问题
Tips of the week 136: unordered containers
保证接口数据安全的10种方案
[C language] string function
Thread pool and singleton mode and file operation
Industry case | digital operation base helps the transformation of life insurance industry
Redis集群与扩展
Hash, bitmap and bloom filter for mass data De duplication
随机推荐
Yunjing network technology interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
万字保姆级长文——Linkedin元数据管理平台Datahub离线安装指南
golang 客户端服务端登录
[论文分享] Where’s Crypto?
【demo】循环队列及条件锁实现goroutine间的通信
Comparison and selection of kubernetes Devops CD Tools
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
How to open an account for wealth securities? Is it safe to open a stock account through the link
Using stored procedures, timers, triggers to solve data analysis problems
标准ACL与扩展ACL
C语言中匿名的最高境界
Wireshark分析抓包数据*.cap
3分钟学会制作动态折线图!
Debian10 compile and install MySQL
线程池的拒绝策略
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
【软件测试】从企业版BOSS直聘,看求职简历,你没被面上是有原因的
Kirk borne's selection of learning resources this week [click the title to download directly]
pip相关命令
Personal best practice demo sharing of enum + validation