我有这样的东西
public static void runThread(Thread t){
ExecutorService threadExecutor = Executors.newSingleThreadExecutor();
threadExecutor.execute(t);
}
如果我这样做Thread.currentThread()
,那么我会回来weblogic.work.ExecuteThread
或有时java.lang.Thread
(我使用 Weblogic 作为我的 AppServer),但如果我这样做
public static void runThread(Thread t){
//ExecutorService threadExecutor = Executors.newSingleThreadExecutor();
//threadExecutor.execute(t);
t.start();
}
然后当我做的时候Thread.currentThread()
,我回来了com.my.thread.JSFExecutionThread
,这是我传入的线程,这就是我想要的。有没有办法修复这样ExecutorService#execute()
返回正确的线程类型Thread#start()
?问题是我想使用 ExecutorService,因为我想利用shutdown()
和shutdownNow()
编辑
这个实现有什么问题吗?
/**
* Run {@code Runnable runnable} with {@code ExecutorService}
* @param runnable {@code Runnable}
* @return
*/
public static ExecutorService runThread(Thread t){
ExecutorService threadExecutor = Executors.newSingleThreadExecutor(
new ExecutionThreadFactory(t));
threadExecutor.execute(t);
return threadExecutor;
}
private static class ExecutionThreadFactory implements ThreadFactory{
private JSFExecutionThread jsfThread;
ExecutionThreadFactory(Thread t){
if(t instanceof JSFExecutionThread){
jsfThread = (JSFExecutionThread)t;
}
}
@Override
public Thread newThread(Runnable r) {
if(jsfThread != null){
return jsfThread;
}else{
return new Thread(r);
}
}
}