当前位置:网站首页>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);
}
});
}
}
边栏推荐
- 【Unity Shader】插入Pass实现模型遮挡X光透视效果
- Yunjing network technology interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
- CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
- The highest level of anonymity in C language
- Wireshark分析抓包数据*.cap
- Kirk borne's selection of learning resources this week [click the title to download directly]
- The performance and efficiency of the model that can do three segmentation tasks at the same time is better than maskformer! Meta & UIUC proposes a general segmentation model with better performance t
- Win11C盘满了怎么清理?Win11清理C盘的方法
- 线程池的拒绝策略
- golang 客户端服务端登录
猜你喜欢

socket编程之常用api介绍与socket、select、poll、epoll高并发服务器模型代码实现

Discuss | what preparations should be made before ar application is launched?

Debian10 compile and install MySQL

Tear the Nacos source code by hand (tear the client source code first)
![[tpm2.0 principle and Application guide] Chapter 5, 7 and 8](/img/38/93fd986916193803bbd90805f832fa.png)
[tpm2.0 principle and Application guide] Chapter 5, 7 and 8

A few simple steps to teach you how to see the K-line diagram

The highest level of anonymity in C language

【软件测试】从企业版BOSS直聘,看求职简历,你没被面上是有原因的

静态路由配置
![[paper sharing] where's crypto?](/img/27/9b47bfcdff8307e63f2699d6a4e1b4.png)
[paper sharing] where's crypto?
随机推荐
Yearning-SQL审核平台
【软件测试】从企业版BOSS直聘,看求职简历,你没被面上是有原因的
Tips for this week 131: special member functions and ` = Default`
[C language] string function
Kubernetes DevOps CD工具对比选型
国内的软件测试会受到偏见吗
“解密”华为机器视觉军团:华为向上,产业向前
Five network IO models
2022年理财产品的一般收益率是多少?
伺服力矩控制模式下的力矩目标值(fTorque)计算
Nat address translation
上市十天就下线过万台,欧尚Z6产品实力备受点赞
Skills of embedded C language program debugging and macro use
AI 击败了人类,设计了更好的经济机制
『HarmonyOS』DevEco的下载安装与开发环境搭建
体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
持续测试(CT)实战经验分享
[论文分享] Where’s Crypto?
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon