java Callable 和 Future 的实际实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14609778/
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
Actual implementation of Callable and Future
提问by Peter
I am in the process of understanding fine grain util.concurrency. Where is implementation of the Java Callable
and Future
located in the JVM ?
我正在了解细粒度 util.concurrency。Java 的实现在哪里Callable
并且Future
位于 JVM 中?
I have found the Future classwhere it describes the future on the high level in Java lang, I am trying to find where it is described on the lower level.
我找到了Future 类,它在 Java lang 的高级别的地方描述了未来,我试图找到它在较低级别的描述位置。
To sum up it would be interesting to find the actual implementation of Future and Callable e.g: the part of the JVM that handles the Future.get() or Callable.call() and prescribes them how they should work.
总而言之,找到 Future 和 Callable 的实际实现会很有趣,例如:JVM 中处理 Future.get() 或 Callable.call() 并规定它们应该如何工作的部分。
Looking forward for your replies, Akonkagva
期待您的回复,Akonkagva
回答by Gray
Where is implementation of the Java Callable and Future located in the JVM ?
JVM 中 Java Callable 和 Future 的实现在哪里?
The main implementation of the Future
interface is the FutureTask
class. It is used by the ExecutorService
classes to represent a submitted job, etc.. Callable
(like Runnable
) is a simple interface that you implement yourself. It wraps a task that you want the ExecutorService
thread-pools to execute. You should download the source jars for these classes and take a look at the Java code yourself.
Future
接口的主要实现是FutureTask
类。ExecutorService
类使用它来表示提交的作业等。 Callable
(如Runnable
)是您自己实现的简单接口。它包装了您希望ExecutorService
线程池执行的任务。您应该下载这些类的源 jars 并自己查看 Java 代码。
Neither of these classes contain any JVM black magic or anything. For example, if you construct a Callable
class, it won't run in another thread unless you submit it to a thread-pool. You can use the Callable
in many different places that have nothing to do with threads.
这些类都不包含任何 JVM 黑魔法或任何东西。例如,如果你构造一个Callable
类,它不会在另一个线程中运行,除非你将它提交到线程池。您可以Callable
在许多与线程无关的地方使用 。
The JVM "black magic" around Future
and Callable
is mostly contained in the Thread
class. It has underlying native support which works with the OS threads to do the actual job of running your task in another thread. There is still a lot of Java code in it if you want to see what it does but there are native and OS calls that the real magic.
JVM的“黑魔法”,围绕Future
并Callable
大多包含在Thread
类。它具有底层本机支持,可与操作系统线程一起完成在另一个线程中运行任务的实际工作。如果你想看看它做了什么,它仍然有很多 Java 代码,但是有真正的魔法的本机和操作系统调用。
Here's a good tutorial about how to use the executor servicesthat were added to Java in 1.5.
这是一个很好的教程,介绍了如何使用1.5 中添加到 Java 中的执行程序服务。
回答by Philipp Wendler
The Guavalibrary has its own implementation of Future
: AbstractFuture
(and subclasses like SettableFuture
) which is an alternative to FutureTask
.
该番石榴库都有自己的执行Future
:AbstractFuture
(等子类SettableFuture
),这是一种替代FutureTask
。
If you are interested in learning how such things are implemented, this might also be interesting to look at. Usually the Guava code is very well written.
如果您有兴趣了解这些事情是如何实现的,那么这可能也很有趣。通常番石榴代码写得很好。
回答by Mikita Belahlazau
Future
is an interface. It has no implementation in itself, it just specify method signatures. You can check source of any of class that implements this interface. Some public classes bundled with JVM are:
Future
是一个接口。它本身没有实现,它只是指定方法签名。您可以检查实现此接口的任何类的来源。一些与 JVM 捆绑的公共类是:
You can use grepcode to see their implementation.
您可以使用 grepcode 来查看它们的实现。