Java 中的并行性
Java 中是否有像 Intel TBB 这样支持并行性的库。
Is there any library like Intel TBB in Java which support Parallelism.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Java 中是否有像 Intel TBB 这样支持并行性的库。
Is there any library like Intel TBB in Java which support Parallelism.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
也许你可以澄清你到底在寻找什么
这就是并发库自 1998 年以来所做的事情,并在 2004 年成为 Java 5.0 的一部分。
编辑:假设您想要一个可以使用系统上所有逻辑处理器的线程池。
这将在每个空闲线程上执行 doWork。
看看它的特征,它们看起来非常熟悉。
我研究了一些低级优化,例如线程感知内存分配。在 Java 中,这称为 TLAB(线程本地分配缓冲区)并且是透明的。我怀疑大多数 Java 开发人员甚至不知道它们的存在。
结果和异常会在 Future 对象中为您捕获,您可以稍后检查。
您可以拥有“条件变量”,例如 CountdownLatch 或 CyclicBarrier
Java的ConcurrentHashMap支持ConcurrentMap和Map接口,允许并发插入和遍历,并且不暴露任何锁。 ;) 它至少已经有 9 年历史了,所以你知道它应该是健壮和稳定的。
如果您愿意,可以在线程池中使用 PriorityQueue。
Perhaps you could clarify what exactly you are looking for
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.
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
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.
有一本关于这个主题的好书:
there is a good book for this topic :
它可以在
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.
看一下 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.
查看 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.
你们中有些人会问并发和并行之间的区别。并发是指不同的程序模拟同时运行,由调度程序决定谁在运行。并行性是指将一个程序划分为多个任务,并且这些任务同时在不同的内核中运行以解决问题。当此任务与其他程序竞争资源时(我们的任务多于可用的核心),那么我们同时具有并发性和并行性。避免竞争条件是并行性中不太困难的部分,最大的问题是找到一种好的方法来正确分解任务以获得良好的结果。非常细粒度的分解有缺点(开销),但粗粒度并行性(单任务)也有缺点(您没有使用计算机上所有可用的处理能力)。你必须找到中期,这意味着做一些优化问题(数学)
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)