Java 是否支持多核处理器/并行处理?
我知道现在大多数处理器都有两个或更多核心,多核编程风靡一时。 Java 中有使用此功能的功能吗?我知道 Java 有一个 Thread 类,但我也知道这个类在多核流行之前已经存在很长时间了。如果我可以在 Java 中使用多核,我会使用什么类/技术?
I know that now that most processors have two or more cores, multicore programming is all the rage. Is there functionality to utilize this in Java? I know that Java has a Thread class, but I also know this was around a long time before multicores became popular. If I can make use of multiple cores in Java, what class/technique would I use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。它也是其他编程语言的平台,其中的实现添加了“真正的多线程”或“真正的线程”卖点。新版本中引入的 G1 垃圾收集器 也利用了多- 核心硬件。
Java 并发实践
尝试获取Java 并发实践一书的副本。
java.util.concurrent
Executors
Executor
是一个简单的标准化接口,用于定义自定义的类线程子系统,包括线程池、异步 IO 和轻量级任务框架。队列
java.util.concurrent
ConcurrentLinkedQueue
类提供了一个高效的可扩展线程安全非阻塞 FIFO 队列。计时
TimeUnit
类提供多种粒度(包括纳秒)来指定和控制基于超时的操作。包中的大多数类都包含基于超时和无限期等待的操作。同步器
四个类有助于常见的专用同步惯用语。
Semaphore
是一个经典的并发工具。CountDownLatch
是一个非常简单但非常常见的实用程序,用于阻塞直到给定数量的信号、事件或条件成立。 [...]并发集合
除了队列之外,此包还提供了一些设计用于多线程上下文的集合实现:
ConcurrentHashMap
、CopyOnWriteArrayList
和CopyOnWriteArraySet
。如果您想要将线程数与可用 CPU 数相匹配,这也会派上用场,例如:
Yes. It also has been a platform for other programming languages where the implementation added a "true multithreading" or "real threading" selling point. The G1 Garbage Collector introduced in newer releases also makes use of multi-core hardware.
Java Concurrency in Practice
Try to get a copy of the Java Concurrency in Practice book.
java.util.concurrent
Executors
Executor
is a simple standardized interface for defining custom thread-like subsystems, including thread pools, asynchronous IO, and lightweight task frameworks.Queues
The java.util.concurrent
ConcurrentLinkedQueue
class supplies an efficient scalable thread-safe non-blocking FIFO queue.Timing
The
TimeUnit
class provides multiple granularities (including nanoseconds) for specifying and controlling time-out based operations. Most classes in the package contain operations based on time-outs in addition to indefinite waits.Synchronizers
Four classes aid common special-purpose synchronization idioms.
Semaphore
is a classic concurrency tool.CountDownLatch
is a very simple yet very common utility for blocking until a given number of signals, events, or conditions hold. [...]Concurrent Collections
Besides Queues, this package supplies a few Collection implementations designed for use in multithreaded contexts:
ConcurrentHashMap
,CopyOnWriteArrayList
, andCopyOnWriteArraySet
.This also comes in handy if you want to match the number of threads to the number of available CPUs for example:
在大多数 Java 实现中,您可以依赖 Java 线程作为真正的操作系统线程。因此,如果您使用 Thread 类,操作系统将负责确保工作负载分布在多个内核上。
操作系统线程早于商用多核系统很长一段时间,因此这实际上并不是一个问题。多核系统的唯一区别是允许时间复用操作系统线程作为真正的并发线程在多个核心上执行。
In most Java implementations, you can rely on Java threads being real OS threads. As a result, the operating system will take care of making sure that the workload is distributed across multiple cores if you use the Thread class.
Operating system threads pre-date commodity multicore systems by a long time, so that's not a concern really. The only difference multicore systems made was to allow time-multiplexed operating system threads to be executed as truly concurrent threads on multiple cores.
Java 5 引入了 java.util.concurrent 包,它有助于构建可以从多核系统中受益的并发应用程序。该包远远超出了 Java 1.4 及更早版本中提供的多线程功能(如同步、等待、通知等)。
有人建议 Java 7 包含 Fork/Join 框架来利用多核系统更容易。
Java 5 introduced the java.util.concurrent package which helps in building concurrent applications that can benefit from multicore systems. This package goes way beyond the multithreading functionality offered in Java 1.4 and earlier (like synchronized, wait, notify, etc).
There's a proposal for Java 7 to include the Fork/Join framework to make use of multicore systems easier.
您将在 Ateji PX 中发现新功能,它是 Java 语言的扩展,具有受 pi 演算启发的并行原语。与线程编程和基于线程的一切(任务、执行器等)完全不同。
与提供对主要硬件级概念的 API 访问的线程库相反,并行性在语言级别引入这种方式,使多核编程更加简单和直观。
这是一种全新的并行编程方法,值得一读(免责声明:我是 Ateji PX 的设计者)。白皮书在这里: http://www .ateji.com/px/whitepapers/Ateji%20PX%20for%20Java%20v1.0.pdf。
You'll find new functionality in Ateji PX, an extension of the Java language with parallel primitives inspired from pi-calculus. Quite different from thread programming and everything thread-based (Tasks, Executors, etc).
Parallelism introduced this way at the language level, as opposed to threading librairies that provide API access to a mostly hardware-level concept, makes multicore programming much simpler and intuitive.
It's a radically new approach to parallel programming worth reading about (disclaimer: I am the designer of Ateji PX). The whitepaper is here : http://www.ateji.com/px/whitepapers/Ateji%20PX%20for%20Java%20v1.0.pdf.
是的。 Java提供并发API利用机器多核处理器的优势。
您可以从运行时获取可用处理器计数,并使用该计数来创建 ExecutorService 通过 执行者。
您还可以使用 ThreadPoolExecutor API达到同样的目的。
Java 8 又提供了一种 API:newWorkStealingPool,它通过使用所有可用的处理器来创建 ForkJoinPool。您不必将处理器计数作为参数传递。
看看代码示例:
看看相关的 SE 问题右执行器:
Java 的 Fork/Join 与 ExecutorService - 何时使用哪个?
Yes. Java provides concurrent API to avail advantages of multi-core processors of machine.
You can get available processors count from Runtime and use that count to create ExecutorService through many APIs in Executors.
You can also use ThreadPoolExecutor API to achieve the same.
Java 8 provides one more API : newWorkStealingPool, which create ForkJoinPool by using all available processors. You don't have to pass processor count as parameter.
Have a look at sample to code:
Have a look at related SE question right Executor:
Java's Fork/Join vs ExecutorService - when to use which?