HeJ小鼠,
我知道如何将参数传递给Runnable。但是当我的Thread运行时,如何获得进程的结果?
class Some implements Runnable
{
int p;
int endresult = 0;
public Some(int param){
p = param;
}
public void run(){
//do something
endresult += p;
//Now how to let the method who executed this runnable know that the result is 2;
}
}
Some s = new Some(1);
Thread t = new Thread(s);
t.start();
当t结束时我想得到'endresult'变量;
答案 0 :(得分:1)
您必须等待线程终止,然后才能直接获取字段值:
t.join();
y = s.endresult;
答案 1 :(得分:1)
声明endresult volatile
并在启动后调用t.join
- 当t完成时,这将获得'endresult'值