Hej,
I know how to pass parameters to a Runnable. But when my Thread has run, how to get the result of the process?
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();
when t is finished i want to get the 'endresult' variable;
You have to wait for your thread to terminate and then you can get the field value directly:
t.join();
y = s.endresult;
declare endresult volatile
and invoke t.join
after it was started - when t is finished this will get the 'endresult' value
精彩评论