简体   繁体   English

Callable和Future的实际实现

[英]Actual implementation of Callable and Future

I am in the process of understanding fine grain util.concurrency. 我正在理解细粒度util.concurrency。 Where is implementation of the Java Callable and Future located in the JVM ? 位于JVM中的Java CallableFuture实现在哪里?

I have found the Future class where 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 eg: the part of the JVM that handles the Future.get() or Callable.call() and prescribes them how they should work. 总而言之,找到Future和Callable的实际实现会很有意思,例如:处理Future.get()或Callable.call()的JVM部分,并规定它们应该如何工作。

Looking forward for your replies, Akonkagva 期待您的回复,Akonkagva

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 . Future接口的主要实现是FutureTask It is used by the ExecutorService classes to represent a submitted job, etc.. Callable (like Runnable ) is a simple interface that you implement yourself. ExecutorService类使用它来表示提交的作业等Callable (如Runnable )是一个您自己实现的简单接口。 It wraps a task that you want the ExecutorService thread-pools to execute. 它包装了您希望ExecutorService线程池执行的任务。 You should download the source jars for these classes and take a look at the Java code yourself. 您应该下载这些类的源jar并亲自查看Java代码。

Neither of these classes contain any JVM black magic or anything. 这些类都不包含任何JVM黑魔法或任何东西。 For example, if you construct a Callable class, it won't run in another thread unless you submit it to a thread-pool. 例如,如果构造一个Callable类,除非将其提交给线程池,否则它不会在另一个线程中运行。 You can use the Callable in many different places that have nothing to do with threads. 您可以在与线程无关的许多不同位置使用Callable

The JVM "black magic" around Future and Callable is mostly contained in the Thread class. FutureCallable的JVM“黑魔法”主要包含在Thread类中。 It has underlying native support which works with the OS threads to do the actual job of running your task in another thread. 它具有底层本机支持,可与OS线程一起完成在另一个线程中运行任务的实际工作。 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. 如果你想看看它的作用,那里仍然有很多Java代码,但是本机和操作系统调用真的很神奇。

Here's a good tutorial about how to use the executor services that were added to Java in 1.5. 这是一个关于如何使用 1.5中添加到Java 的执行程序服务好教程

The Guava library has its own implementation of Future : AbstractFuture (and subclasses like SettableFuture ) which is an alternative to FutureTask . Guava库有自己的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. 通常Guava代码写得很好。

Future is an interface. Future是一个界面。 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: 与JVM捆绑在一起的一些公共类是:

You can use grepcode to see their implementation. 您可以使用grepcode查看其实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM