Java j2me从运行线程获取结果
Hej,
我知道如何将参数传递给 Runnable。但是当我的线程运行后,如何获取进程的结果呢?
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' 变量;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须等待线程终止,然后才能直接获取字段值:
You have to wait for your thread to terminate and then you can get the field value directly:
声明 endresult
volatile
并在启动后调用t.join
- 当 t 完成时,这将获得“endresult”值declare endresult
volatile
and invoket.join
after it was started - when t is finished this will get the 'endresult' value