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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:48:59  来源:igfitidea点击:

Actual implementation of Callable and Future

javaconcurrencytheoryfuturecallable

提问by Peter

I am in the process of understanding fine grain util.concurrency. Where is implementation of the Java Callableand Futurelocated 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 Futureinterface is the FutureTaskclass. It is used by the ExecutorServiceclasses 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 ExecutorServicethread-pools to execute. You should download the source jars for these classes and take a look at the Java code yourself.

Future接口的主要实现是FutureTaskExecutorService类使用它来表示提交的作业等。 Callable(如Runnable)是您自己实现的简单接口。它包装了您希望ExecutorService线程池执行的任务。您应该下载这些类的源 jars 并自己查看 Java 代码。

Neither of these classes contain any JVM black magic or anything. For example, if you construct a Callableclass, it won't run in another thread unless you submit it to a thread-pool. You can use the Callablein many different places that have nothing to do with threads.

这些类都不包含任何 JVM 黑魔法或任何东西。例如,如果你构造一个Callable类,它不会在另一个线程中运行,除非你将它提交到线程池。您可以Callable在许多与线程无关的地方使用 。

The JVM "black magic" around Futureand Callableis mostly contained in the Threadclass. 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的“黑魔法”,围绕FutureCallable大多包含在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.

番石榴库都有自己的执行FutureAbstractFuture(等子类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

Futureis 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 来查看它们的实现。