当前位置:网站首页>线程池中的线程工厂
线程池中的线程工厂
2022-07-07 16:37:00 【珠峰下的沙砾】
既然都使用的线程池,则创建线程的工作都交付给了线程池去完成。我们只需要告诉线程池需要多少个线程,剩下的工作都交于线程池完成。在线程池中创建线程使用的是线程工厂,
下面我们看看有哪些线程工厂:
ThreadFactory(父级接口)
public interface ThreadFactory {
// 构造一个新的Thread 。实现还可以初始化优先级、名称、守护进程状态、 ThreadGroup等
Thread newThread(Runnable r);
}
DefaultThreadFactory(默认使用)
// 默认线程工厂
static class DefaultThreadFactory implements ThreadFactory {
// 线程池号
private static final AtomicInteger poolNumber = new AtomicInteger(1);
// 线程组
private final ThreadGroup group;
// 线程号
private final AtomicInteger threadNumber = new AtomicInteger(1);
// 线程名称前缀
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
// 获取线程组
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
// 线程名称前缀 = pool + 线程池号 + thread
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
// 创建新的线程
public Thread newThread(Runnable r) {
// 创建线程(线程组,任务,线程名称)
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
// 判断当前查询是否是守护线程
// 如果是守护线程则设置为非守护线程
if (t.isDaemon())
t.setDaemon(false);
// 判断线程的优先级是否为默认优先级
if (t.getPriority() != Thread.NORM_PRIORITY)
// 将线程优先级设置为默认优先级
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
PrivilegedThreadFactory
// 特权线程工厂
// 线程工厂捕获访问控制上下文和类加载器
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);
}
});
}
}
边栏推荐
- Tips for short-term operation of spot silver that cannot be ignored
- [unity shader] insert pass to realize the X-ray perspective effect of model occlusion
- Summary of evaluation indicators and important knowledge points of regression problems
- Taffydb open source JS database
- What skills can you master to be a "master tester" when doing software testing?
- Kubernetes DevOps CD工具对比选型
- Industry case | digital operation base helps the transformation of life insurance industry
- 性能测试过程和计划
- “解密”华为机器视觉军团:华为向上,产业向前
- Performance test process and plan
猜你喜欢

Debian10 compile and install MySQL

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

Introduction to OTA technology of Internet of things

Download, installation and development environment construction of "harmonyos" deveco

ICer知识点杂烩(后附大量题目,持续更新中)

< code random recording two brushes> linked list

socket編程之常用api介紹與socket、select、poll、epoll高並發服務器模型代碼實現
![[paper sharing] where's crypto?](/img/27/9b47bfcdff8307e63f2699d6a4e1b4.png)
[paper sharing] where's crypto?

磁盘存储链式的B树与B+树

String type, constant type and container type of go language
随机推荐
[tpm2.0 principle and Application guide] Chapter 5, 7 and 8
讨论| 坦白局,工业 AR 应用为什么难落地?
[principle and technology of network attack and Defense] Chapter 6: Trojan horse
sqlite sql 异常 near “with“: syntax error
Nunjuks template engine
Unlike the relatively short-lived industrial chain of consumer Internet, the industrial chain of industrial Internet is quite long
How to clean when win11 C disk is full? Win11 method of cleaning C disk
Personal best practice demo sharing of enum + validation
讨论 | AR 应用落地前,要做好哪些准备?
保证接口数据安全的10种方案
nest. Database for getting started with JS
低代码助力企业数字化转型会让程序员失业?
现在网上期货开户安全吗?国内有多少家正规的期货公司?
能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...
Afghan interim government security forces launched military operations against a hideout of the extremist organization "Islamic state"
Chapter 1 Introduction to CRM core business
同消费互联网的较为短暂的产业链不同,产业互联网的产业链是相当漫长的
Kirk Borne的本周学习资源精选【点击标题直接下载】
Backup Alibaba cloud instance OSS browser
海量数据去重的hash,bitmap与布隆过滤器Bloom Filter