开发者

Java j2me get result from running Thread

开发者 https://www.devze.com 2023-04-01 21:04 出处:网络
Hej, I know how to pass parameters to a Runnable. But when my Thread has run, how to get the result of the process?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消