4

有没有像 Java 中的 Intel TBB 这样支持并行的库。

4

6 回答 6

8

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

英特尔® 线程构建模块(英特尔 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。

于 2011-01-05T15:42:14.153 回答
6

这个主题有一本好书:

Java Concurrency in Practice

Java 并发实践

于 2011-01-05T15:17:39.797 回答
4

java.util.concurrent从 Java5 开始,它在包中可用。

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

于 2011-01-05T15:09:37.483 回答
1

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

于 2011-01-05T15:17:47.167 回答
0

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

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

于 2011-01-05T15:09:42.070 回答
0

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)

于 2011-09-29T17:50:23.237 回答