Java 中的并行性

发布于 2024-10-10 04:27:09 字数 38 浏览 7 评论 0原文

Java 中是否有像 Intel TBB 这样支持并行性的库。

Is there any library like Intel TBB in Java which support Parallelism.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

不弃不离 2024-10-17 04:27:09

也许你可以澄清你到底在寻找什么

英特尔® 线程构建模块(英特尔 TBB)提供了丰富而完整的方法来在 C++ 程序中表达并行性。它是一个可以帮助您充分利用多核处理器性能的库,而无需成为线程专家。英特尔 TBB 不仅仅是一个线程替换库。它代表了更高级别的、基于任务的并行性,抽象了平台细节和线程机制以实现可扩展性和性能。

这就是并发库自 1998 年以来所做的事情,并在 2004 年成为 Java 5.0 的一部分。

编辑:假设您想要一个可以使用系统上所有逻辑处理器的线程池。

ExecutorService es = Executors.newFixedThreadPool(
      Runtime.getRuntime().availableProcessors);

// to add a number of tasks
for(int i=0; i<numberOfTasks; i++) {
   es.submit(new Callable<ResultType>() {
      public ResultType call() {
          return doWork(i);
      }
   }
}

这将在每个空闲线程上执行 doWork。

看看它的特征,它们看起来非常熟悉。

我研究了一些低级优化,例如线程感知内存分配。在 Java 中,这称为 TLAB(线程本地分配缓冲区)并且是透明的。我怀疑大多数 Java 开发人员甚至不知道它们的存在。

结果和异常会在 Future 对象中为您捕获,您可以稍后检查。

您可以拥有“条件变量”,例如 CountdownLatch 或 CyclicBarrier

模仿 C++ 0x unordered_map 的新容器,基于 Intel (TBB 3.0) 和 Microsoft (Visual Studio 2010) 实施的联合规范。与之前的concurrent_hash_map相比,它具有三个优点:

  • 与C++0x unordered_map非常相似的接口
  • 它允许并发插入和遍历。
  • 接口不公开任何锁定。实现可以在内部使用锁,但锁定永远不会以可能导致死锁的方式公开。它可能会在内部持有锁,但在调用用户定义的代码时绝不会持有锁。

Java的ConcurrentHashMap支持ConcurrentMap和Map接口,允许并发插入和遍历,并且不暴露任何锁。 ;) 它至少已经有 9 年历史了,所以你知道它应该是健壮和稳定的。

如果您愿意,可以在线程池中使用 PriorityQueue。

Perhaps you could clarify what exactly you are looking for

Intel® Threading Building Blocks (Intel TBB) offers a rich and complete approach to expressing parallelism in a C++ program. It is a library that helps you take advantage of multi-core processor performance without having to be a threading expert. Intel TBB is not just a threads-replacement library. It represents a higher-level, task-based parallelism that abstracts platform details and threading mechanisms for scalability and performance.

This is what the concurrency libraries have done since 1998 and were made part of Java 5.0 in 2004.

EDIT: Say you want a thread pool which can use all the logical processors on your system.

ExecutorService es = Executors.newFixedThreadPool(
      Runtime.getRuntime().availableProcessors);

// to add a number of tasks
for(int i=0; i<numberOfTasks; i++) {
   es.submit(new Callable<ResultType>() {
      public ResultType call() {
          return doWork(i);
      }
   }
}

This will performs doWork on each free thread.

Looking at its features they look very familiar.

I had a look at some of the low level optimisations like thread aware memory allocation. In Java this is called TLAB (Thread Local Allocation Buffers) and is transparent. I suspect most Java developers don't even know they exist.

Results and Exceptions are captured for you in a Future object which you can inspect later.

You can have "condition variables" such as CountdownLatch or CyclicBarrier

A new container mimicking C++ 0x unordered_map and based on a joint specification implemented by both Intel (TBB 3.0) and Microsoft (Visual Studio 2010). It has three advantages over the previous concurrent_hash_map:

  • An interface closely resembling the C++0x unordered_map
  • It permits concurrent insertion and traversal.
  • No locking is exposed by the interface. An implementation may use locks internally, but the locking is never exposed in a way that can contribute to deadlock. It may hold locks internally, but never while calling user defined code.

Java's ConcurrentHashMap supports the ConcurrentMap and Map interfaces, permits concurrent insertion and traversal and does not expose any locks. ;) It is at least 9 years old so you know it should be robust and stable.

There is a PriorityQueue which you can use in you thread pool if you wish.

痕至 2024-10-17 04:27:09

有一本关于这个主题的好书:

Java Concurrency in Practice

Java Concurrency in Practice

there is a good book for this topic :

Java Concurrency in Practice

Java Concurrency in Practice

骑趴 2024-10-17 04:27:09

它可以在 java 中找到.util.concurrent 包,自 Java5 起。

该版本对 Java 内存模型进行了重大修改,以巩固并发编程的基础并修复已知问题。在 Java 中,并发性从一开始就被设计到语言中,而不是简单地稍后添加到库中。

It is available in the java.util.concurrent package, since Java5.

That edition included a major overhaul of the Java memory model, to solidify the basis of concurrent programming and fix known problems. In Java, concurrency is designed into the language from the start, not simply added later in a library.

一口甜 2024-10-17 04:27:09

看一下 akka,它提供了一个使用 Actor 和软件事务内存的并发框架。当然,这只是提供了构建块,您仍然需要能够编写一些并发代码,但它变得更容易一些。

Take a look at akka which provides a concurrency framework using Actors and software transactional memory. Of course, this just provides building blocks, you still need to be able to write some concurrent code, but it becomes a little easier.

拿命拼未来 2024-10-17 04:27:09

查看 java.util.concurrent 包。它有一大堆对并行编程有用的类。

顺便说一句,您应该考虑接受一些答案。 15 个问题没有被接受的答案会阻碍人们回答你的问题。

Check out the java.util.concurrent package. It has a whole bunch of classes that are useful for parallel programming.

By the way, you should consider accepting some answers. 15 questions with no accepted answers discourages people from answering your questions.

风流物 2024-10-17 04:27:09

你们中有些人会问并发和并行之间的区别。并发是指不同的程序模拟同时运行,由调度程序决定谁在运行。并行性是指将一个程序划分为多个任务,并且这些任务同时在不同的内核中运行以解决问题。当此任务与其他程序竞争资源时(我们的任务多于可用的核心),那么我们同时具有并发性和并行性。避免竞争条件是并行性中不太困难的部分,最大的问题是找到一种好的方法来正确分解任务以获得良好的结果。非常细粒度的分解有缺点(开销),但粗粒度并行性(单任务)也有缺点(您没有使用计算机上所有可用的处理能力)。你必须找到中期,这意味着做一些优化问题(数学)

Some of you are asking about the difference between concurrency and parallelism. Concurency means different programs that simulate running at the same time and the scheduler decides who is the one is running. Parallelism means one program divided into tasks and those task running at the same time in different cores to solve a problem. When this tasks comptete for resources with other programs (we have more tasks than cores available) then we have concurrency and parallelism at the same time. The avoing race conditions is the less difficult part in parallelism, the big problem is to find a good way to decompose a task properly to get good results. Very fine grained decompostion has drawbacks (overheads) but coarse-grained parallelism (monotasking) has drawbacks too (you are not using all processing power available on the computer). You have to find the medium term, it implies doing some optimization problems (mathematics)

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