Java 是否支持多核处理器/并行处理? [英] Does Java have support for multicore processors/parallel processing?

查看:24
本文介绍了Java 是否支持多核处理器/并行处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道现在大多数处理器都有两个或更多内核,多核编程风靡一时.是否有在 Java 中使用它的功能?我知道 Java 有一个 Thread 类,但我也知道这是在多核流行之前的很长一段时间.如果我可以在 Java 中使用多个内核,我会使用什么类/技术?

解决方案

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

是的.它也是其他编程语言的平台,其中的实现增加了真正的多线程"或真正的线程"卖点.新版本中引入的 G1 Garbage Collector 也利用了多-核心硬件.

Java 并发实践

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

<小时><块引用>

如果我可以使用多个内核Java,我会使用什么课程/技术用吗?

java.util.concurrent

<块引用>

实用程序类通常用于并发编程.这个包包括一些小的标准化可扩展的框架,以及一些提供有用的类功能,否则繁琐或难以实施.以下是对主要组件.

执行者

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

队列

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

时间

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

同步器

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

并发集合

除了队列之外,这个包还提供了一些专为在多线程上下文中使用而设计的集合实现:ConcurrentHashMapCopyOnWriteArrayListCopyOnWriteArraySet.<小时>

如果您想将线程数与可用 CPU 数相匹配,这也很方便,例如:

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

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?

解决方案

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();

这篇关于Java 是否支持多核处理器/并行处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆