当前位置:网站首页>线程池中的线程工厂
线程池中的线程工厂
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);
}
});
}
}
边栏推荐
- 现在网上期货开户安全吗?国内有多少家正规的期货公司?
- 用存储过程、定时器、触发器来解决数据分析问题
- String type, constant type and container type of go language
- 讨论| 坦白局,工业 AR 应用为什么难落地?
- Mobile pixel bird game JS play code
- 财富证券证券怎么开户?通过链接办理股票开户安全吗
- 单臂路由和三层交换的简单配置
- [tpm2.0 principle and Application guide] Chapter 5, 7 and 8
- 低代码助力企业数字化转型会让程序员失业?
- CVPR 2022丨学习用于小样本语义分割的非目标知识
猜你喜欢
Classification of regression tests
现货白银分析中的一些要点
Skills of embedded C language program debugging and macro use
debian10编译安装mysql
Backup Alibaba cloud instance OSS browser
Learn to make dynamic line chart in 3 minutes!
The highest level of anonymity in C language
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
保证接口数据安全的10种方案
Chapter 3 business function development (user access project)
随机推荐
『HarmonyOS』DevEco的下载安装与开发环境搭建
[tpm2.0 principle and Application guide] Chapter 5, 7 and 8
用存储过程、定时器、触发器来解决数据分析问题
直播软件搭建,canvas文字加粗
AI 击败了人类,设计了更好的经济机制
2021年全国平均工资出炉,你达标了吗?
Summary of debian10 system problems
云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
【剑指 Offer】59 - I. 滑动窗口的最大值
2022年理财产品的一般收益率是多少?
Learn to make dynamic line chart in 3 minutes!
【C语言】字符串函数
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
上市十天就下线过万台,欧尚Z6产品实力备受点赞
Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
[principles and technologies of network attack and Defense] Chapter 5: denial of service attack
Win11C盘满了怎么清理?Win11清理C盘的方法
Wireshark分析抓包数据*.cap
[principle and technology of network attack and Defense] Chapter 6: Trojan horse
Chapter 3 business function development (to remember account and password)