当前位置:网站首页>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);
}
});
}
}
边栏推荐
- Personal best practice demo sharing of enum + validation
- Reinforcement learning - learning notes 8 | Q-learning
- Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
- AntiSamy:防 XSS 攻击的一种解决方案使用教程
- Learn to make dynamic line chart in 3 minutes!
- Debian10 compile and install MySQL
- 【塔望方法论】塔望3W消费战略 - U&A研究法
- [PaddleSeg源码阅读] PaddleSeg Validation 中添加 Boundary IoU的计算(1)——val.py文件细节提示
- 线程池中的线程工厂
- 将模型的记忆保存下来!Meta&UC Berkeley提出MeMViT,建模时间支持比现有模型长30倍,计算量仅增加4.5%...
猜你喜欢
[principle and technology of network attack and Defense] Chapter 1: Introduction
Using stored procedures, timers, triggers to solve data analysis problems
Idea completely uninstalls installation and configuration notes
现货白银分析中的一些要点
线程池和单例模式以及文件操作
保证接口数据安全的10种方案
Summary of debian10 system problems
Skills of embedded C language program debugging and macro use
静态路由配置
Thread pool and singleton mode and file operation
随机推荐
Redis集群与扩展
云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
Tear the Nacos source code by hand (tear the client source code first)
Download, installation and development environment construction of "harmonyos" deveco
[principle and technology of network attack and Defense] Chapter 6: Trojan horse
Industry case | digital operation base helps the transformation of life insurance industry
gsap动画库
标准ACL与扩展ACL
Discuss | what preparations should be made before ar application is launched?
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
PIP related commands
【塔望方法论】塔望3W消费战略 - U&A研究法
[paddleseg source code reading] add boundary IOU calculation in paddleseg validation (1) -- val.py file details tips
Unlike the relatively short-lived industrial chain of consumer Internet, the industrial chain of industrial Internet is quite long
4种常见的缓存模式,你都知道吗?
【demo】循环队列及条件锁实现goroutine间的通信
Tips of the week 136: unordered containers
强化学习-学习笔记8 | Q-learning
NAT地址转换
Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)