ThreadFactory接口,只有一个public方法 newThread。
继承这个接口,可以自己的ThreadFactory,此例为Thread添加一个自己的前缀。
DaemonThreadFactory
/**
*
*/
public class DaemonThreadFactory implements ThreadFactory {
//
private String prefix;
private volatile int threadId;
/**
*
* @param prefix
*/
public DaemonThreadFactory(String prefix) {
this.prefix = prefix;
}
/**
*
*/
public Thread newThread(Runnable runnable) {
Thread r = new Thread(runnable);
r.setName(prefix + (threadId++));
r.setDaemon(true);
return r;
}
}
接合Executors使用.
例如.Executors.newSingleThreadExecutor(ThreadFactory threadFactory);
ExecutorService executor = Executors.newSingleThreadExecutor(new DaemonThreadFactory());
executor.submit(new Runnable() {
public void run() {
try {
//do some thing
} catch (Throwable e) {
}
}
});
分享到:
相关推荐
AboutTime – 古老但实用的网络校时软件
It is a practice about thread scheduling. Threads can have 5 status: running, ready, block, wait, dead.
about c#.doc 关于C# 的简介 及 发展前景 about c#.doc 关于C# 的简介 及 发展前景 about c#.doc 关于C# 的简介 及 发展前景
Android About Page Create an awesome About Page for your Android App in 2 minutes This library allows to generate beautiful About Pages with less effort, it's fully customizable and supports opening ...
thread 初级入门
disagree about disagrees about version
AboutTime 时间同步,也可在局域网内做时间服务器
about downloads on macbook pro
All About PDF 顾名思义是包含了PDF工具相关的一切功能,所以All About PDF是一款包含具备PDF合并、PDF分割、PDF保护、页面删除、水印添加、PDF解锁、PDF转换、PDF压缩等多种实用功能于一体的多功能综合性PDF工具箱...
econ sta math hedge funds easy way about hedge funds
一款古老、高效、精准的校时小工具。AboutTime_4.8,附带能用的校时站点。
很简洁又不失大气的关于我们(about)模板
ntp校时软件windows版本,AboutTime 是一款比较古老的软件了,尽管如此,它以其强大的功能和精巧高效,仍被很多人喜爱和使用。它既可作为校时客户端为本机校时,也可在局域网中作为校时服务器运行。常用于windows...
Law What about the Future
AboutTime是时间对时软件 能够快速有效的和同网电脑对时同步等
about.html
Joseph Y. Halpern, Reasoning about Uncertainty 2nd Edn, 2017 不确定性 推理 好书,新版
100 Things Every Designer Needs To Know About People (2011)
Jounral of indexed by SCI about artificial neural networks.