【发布时间】:2016-06-23 02:01:16
【问题描述】:
我有一个具有属性的自定义线程类。所述属性从自定义 ThreadFactory 传递。我可以从 Runnable 任务中以某种方式获取线程对象的该属性吗?
显示想法的示例代码(可能有错误):
public static void main(String[] args){
MyFactory factory = new MyFactory("abc");
ExecutorService e = Executors.newFixedThreadPool(4,factory);
MyRunnable r = new MyRunnable();
e.submit(r);
}
public class MyFactory implements ThreadFactory{
private String property;
public MyFactory(String p){
property = p;
}
public MyThread newThread(Runnable r){
MyThread out = new MyThread(r, property);
}
}
public class MyThread extends Thread{
private String property;
public MyThread(Runnable r, String p){
super(r);
property = p;
}
public String getProperty(){
return property;
}
}
public class MyRunnable implements Runnable{
public void run(){
/* Can I get my "abc" property from >>HERE<< ? */
{
{
【问题讨论】:
标签: java multithreading threadpool