Reputation: 1637
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;
Upvotes: 1
Views: 333
Reputation: 6214
declare endresult volatile
and invoke t.join
after it was started - when t is finished this will get the 'endresult' value
Upvotes: 1
Reputation: 39197
You have to wait for your thread to terminate and then you can get the field value directly:
t.join();
y = s.endresult;
Upvotes: 1