Java 是否支持多核处理器/并行处理?

发布于 2024-09-11 04:36:33 字数 131 浏览 5 评论 0原文

我知道现在大多数处理器都有两个或更多核心,多核编程风靡一时。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒绊 2024-09-18 04:36:33

Java是否支持多核
处理器/并行处理?

是的。它也是其他编程语言的平台,其中的实现添加了“真正的多线程”或“真正的线程”卖点。新版本中引入的 G1 垃圾收集器 也利用了多- 核心硬件。

Java 并发实践

尝试获取Java 并发实践一书的副本。


如果我可以利用多个核心
Java,我会学习什么课程/技术
使用?

java.util.concurrent

实用程序类通常用于
并发编程。这个套餐
包括一些小的标准化
可扩展的框架,以及一些
提供有用的类
功能和其他方面
乏味或难以实施。
以下是对以下内容的简要描述
主要部件。

Executors

Executor 是一个简单的标准化接口,用于定义自定义的类线程子系统,包括线程池、异步 IO 和轻量级任务框架。

队列

java.util.concurrent ConcurrentLinkedQueue 类提供了一个高效的可扩展线程安全非阻塞 FIFO 队列。

计时

TimeUnit 类提供多种粒度(包括纳秒)来指定和控制基于超时的操作。包中的大多数类都包含基于超时和无限期等待的操作。

同步器

四个类有助于常见的专用同步惯用语。 Semaphore 是一个经典的并发工具。 CountDownLatch 是一个非常简单但非常常见的实用程序,用于阻塞直到给定数量的信号、事件或条件成立。 [...]

并发集合

除了队列之外,此包还提供了一些设计用于多线程上下文的集合实现:ConcurrentHashMapCopyOnWriteArrayListCopyOnWriteArraySet


如果您想要将线程数与可用 CPU 数相匹配,这也会派上用场,例如:

int n = Runtime.getRuntime().availableProcessors();

Does Java have support for multicore
processors/parallel processing?

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.


If I can make use of multiple cores in
Java, what class/technique would I
use?

java.util.concurrent

Utility classes commonly useful in
concurrent programming. This package
includes a few small standardized
extensible frameworks, as well as some
classes that provide useful
functionality and are otherwise
tedious or difficult to implement.
Here are brief descriptions of the
main components.

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, and CopyOnWriteArraySet.


This also comes in handy if you want to match the number of threads to the number of available CPUs for example:

int n = Runtime.getRuntime().availableProcessors();
还给你自由 2024-09-18 04:36:33

在大多数 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.

司马昭之心 2024-09-18 04:36:33

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.

找个人就嫁了吧 2024-09-18 04:36:33

您将在 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.

眼泪也成诗 2024-09-18 04:36:33

是的。 Java提供并发API利用机器多核处理器的优势。

您可以从运行时获取可用处理器计数,并使用该计数来创建 ExecutorService 通过 执行者

您还可以使用 ThreadPoolExecutor API达到同样的目的。

Java 8 又提供了一种 API:newWorkStealingPool,它通过使用所有可用的处理器来创建 ForkJoinPool。您不必将处理器计数作为参数传递。

看看代码示例:

int processorCount = Runtime.getRuntime().availableProcessors();

ExecutorService executor1 = Executors.newFixedThreadPool(processorCount);
ExecutorService executor2 =  
                            Executors.newScheduledThreadPool(processorCount);
ExecutorService executor3 = Executors.newWorkStealingPool(); // java-8 API
ForkJoinPool forkJoinPool = new ForkJoinPool(processorCount);

看看相关的 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:

int processorCount = Runtime.getRuntime().availableProcessors();

ExecutorService executor1 = Executors.newFixedThreadPool(processorCount);
ExecutorService executor2 =  
                            Executors.newScheduledThreadPool(processorCount);
ExecutorService executor3 = Executors.newWorkStealingPool(); // java-8 API
ForkJoinPool forkJoinPool = new ForkJoinPool(processorCount);

Have a look at related SE question right Executor:

Java's Fork/Join vs ExecutorService - when to use which?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文