java如何查看从未来对象执行的线程(名称)
下面的代码是我创建submit to executor服务实例的代码,其结果是我存储在未来对象中的代码。是否有任何方法可以看到从未来对象中给出结果的线程的名称。例如,如果线程1返回一个整数值4,并且该值存储在未来的对象中。我如何判断线程1是执行并返回值4的线程?如果我没有正确解释,请随时澄清
class Test implements Callable<Integer>{
Integer i;
String threadName;
public Test(Integer i){
this.i = i;
}
public Integer call() throws Exception{
threadName = Thread.currentThread().getName();
System.out.println(Thread.currentThread().getName());
Thread.sleep(i * 1000);
return i ;
}
public String toString(){
return threadName;
}
}
# 1 楼答案
您可以返回包含结果和线程名称的对象,而不是
Integer
: