如何从 Java 中的内部 Thread Runnable 方法获取返回值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7714432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i get returned value from inner Thread Runnable method in Java?
提问by Kru
How do i assign Status
with CallMe()
using isFinish()
to have returned value true?
我如何分配Status
与CallMe()
使用isFinish()
有返回值true?
public static boolean isFinish ()
{
boolean Status = false;
new Thread(new Runnable()
{
public void run()
{
/* This shell return true or false
* How do you keep it in Status
*/
CallMe();
}
}).start();
/* How can i get the true or false exactly from CallMe? here */
return Status;
}
public static boolean CallMe()
{
/* some heavy loads ... */
return true;
}
回答by Kru
There are two ways of doing this. The first is to use a future computation result and the other is to have a shared variable. I think that the first method is much cleaner than the second, but sometimes you need to push values to the thread too.
有两种方法可以做到这一点。第一个是使用未来的计算结果,另一个是有一个共享变量。我认为第一种方法比第二种方法更简洁,但有时您也需要将值推送到线程。
- Using a
RunnableFuture
.
- 使用
RunnableFuture
.
FutureTask
implements a RunnableFuture
. So you create that task which, once executed, will have a value.
FutureTask
实现一个RunnableFuture
. 所以你创建了那个任务,一旦执行,它就会有一个值。
RunnableFuture f = new FutureTask(new Callable<Boolean>() {
// implement call
});
// start the thread to execute it (you may also use an Executor)
new Thread(f).start();
// get the result
f.get();
- Using a holder class
- 使用持有者类
You create a class holding a value and share a reference to that class. You may create your own class or simply use the AtomicReference
.
By holder class, I mean a class that has a public modifiable attribute.
您创建一个持有值的类并共享对该类的引用。您可以创建自己的类或简单地使用AtomicReference
. 通过持有者类,我的意思是一个具有公共可修改属性的类。
// create the shared variable
final AtomicBoolean b = new AtomicBoolean();
// create your thread
Thread t = new Thread(new Runnable() {
public void run() {
// you can use b in here
}
});
t.start();
// wait for the thread
t.join();
b.get();
回答by Edwin Buck
You rewrite the code to use Callable<Boolean>
and obtain a Future
when launching the Runnable
.
您重写代码以使用Callable<Boolean>
并Future
在启动Runnable
.
Futures allow the launching thread to properly check that the value is ready and read it asynchronously. You could do the coding by hand, but since Future
is now part of the standard JVM libraries, why would you (outside of a programming class)?
Futures 允许启动线程正确检查值是否准备好并异步读取它。您可以手动编写代码,但既然Future
现在是标准 JVM 库的一部分,为什么要(在编程类之外)呢?
回答by Andy Thomas
Working with raw threads, you could implement Runnable with a named type, and store the value in it.
使用原始线程,您可以使用命名类型实现 Runnable,并将值存储在其中。
class MyRunnable implements Runnable {
boolean status;
public void run() {
...
}
}
However, if you're working with another thread, you'll have to synchronize in some way.
但是,如果您正在使用另一个线程,则必须以某种方式进行同步。
It would be easier to use the higher-level tools provided by the java.util.concurrent hierarchy. You can submit a Callable to an Executor, and get a Future. You can ask the Future if it's done, and get the result. There's an Oracle tutorial here.
使用 java.util.concurrent 层次结构提供的更高级别的工具会更容易。您可以将 Callable 提交给 Executor,并获得 Future。你可以询问 Future 是否完成,并得到结果。这里有一个Oracle 教程。