当前位置:网站首页>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);
}
});
}
}
边栏推荐
- [trusted computing] Lesson 12: TPM authorization and conversation
- Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
- Reinforcement learning - learning notes 8 | Q-learning
- Hash, bitmap and bloom filter for mass data De duplication
- Kubernetes DevOps CD工具对比选型
- Tear the Nacos source code by hand (tear the client source code first)
- 强化学习-学习笔记8 | Q-learning
- 通过 Play Integrity API 的 nonce 字段提高应用安全性
- [C language] string function
- 行业案例|数字化经营底座助力寿险行业转型
猜你喜欢

The highest level of anonymity in C language

Thread pool and singleton mode and file operation

讨论| 坦白局,工业 AR 应用为什么难落地?

socket编程之常用api介绍与socket、select、poll、epoll高并发服务器模型代码实现
![[Tawang methodology] Tawang 3W consumption strategy - U & a research method](/img/63/a8c08ac6ec7d654159e5fc8b4423e4.png)
[Tawang methodology] Tawang 3W consumption strategy - U & a research method

直播预约通道开启!解锁音视频应用快速上线的秘诀

Kirk Borne的本周学习资源精选【点击标题直接下载】

Year SQL audit platform

『HarmonyOS』DevEco的下载安装与开发环境搭建

手撕Nacos源码(先撕客户端源码)
随机推荐
行业案例|数字化经营底座助力寿险行业转型
现在网上期货开户安全吗?国内有多少家正规的期货公司?
静态路由配置
Kirk Borne的本周学习资源精选【点击标题直接下载】
AntiSamy:防 XSS 攻击的一种解决方案使用教程
Personal best practice demo sharing of enum + validation
Is it safe to open an online futures account now? How many regular futures companies are there in China?
【demo】循环队列及条件锁实现goroutine间的通信
用存储过程、定时器、触发器来解决数据分析问题
Disk storage chain B-tree and b+ tree
[C language] string function
企业展厅设计中常用的三种多媒体技术形式
Afghan interim government security forces launched military operations against a hideout of the extremist organization "Islamic state"
Ten thousand words nanny level long article -- offline installation guide for datahub of LinkedIn metadata management platform
来了!GaussDB(for Cassandra)新特性亮相
[trusted computing] Lesson 13: TPM extended authorization and key management
低代码助力企业数字化转型会让程序员失业?
RIP和OSPF的区别和配置命令
Download, installation and development environment construction of "harmonyos" deveco
idea彻底卸载安装及配置笔记