public class MyThreadFactory implements ThreadFactory{
private final String poolName;
public MyThreadFactory(String poolName){
this.poolName = poolName;
}
ppublic Thread newThread(Runnable runnable){
return new MyAppThread(runnable,poolName);
}
}
public class MyAppThread extends Thread{
public static final String DEFAULT_NAME = "MyAppThread";
private static volatile boolean debugLifecycle = false;
private static final AtomicInteger created = new AtomicInteger();
private static final AtomicInteger alive = new AtomicInteger();
private static final Logger log = Logger.getAnonymousLogger();
public MyAppThread(Runnable runnable){
this(runnable,DEFAULT_NAME);
}
public MyAppThread(Runnable runnable,String name){
super(runnable,name+"-"+created.incrementAndGet());
setUncaughtExceptionHandler{
new Thread.UncaughtException(Thread t,Throwable e){
log.log(Level.SEVERE,"UNCAUGHT IN the");
}
}
}
public void run(){
boolean dubug = debugLifcycle;
try{
alive.incrementAndGet();
super.run();
}finally{
alive.decrementAndGet();
if(){
log.log();
}
}
}
}
自定义线程工厂JAVA
最新推荐文章于 2025-10-29 10:13:29 发布
该博客介绍了一个名为`MyThreadFactory`的自定义线程工厂类,它用于创建具有特定名称的线程。`MyAppThread`是线程的扩展,增加了线程生命周期的日志记录和异常处理。当线程运行结束时,会记录相关信息。
884

被折叠的 条评论
为什么被折叠?



