Java 中等效的并行扩展

发布于 2024-07-25 15:24:35 字数 90 浏览 12 评论 0原文

我在 .Net 开发中使用并行扩展有一些经验,但我正在考虑在 Java 中做一些工作,这些工作将受益于易于使用的并行库。 JVM 是否提供任何与并行扩展类似的工具?

I have some experience using parallel extensions in .Net development, but I was looking at doing some work in Java that would benefit from an easy to use parallelism library. Does the JVM offer any comparable tools to parallel-extensions?

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

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

发布评论

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

评论(4

如日中天 2024-08-01 15:24:35

您应该熟悉 java.util.concurrent 包。 它具有简单而强大的并行编程原语,并以此为基础的高级库。 此类库的一个示例是 Functional Java,它具有 一个易于使用的并行编程模块

例如,下面是使用 Function Java 编写的规范 MapReduce 示例。 它计算一组文档中的单词数,假设文档是字符流:

public static long countWords(final List<Stream<Character>> documents,
                              final ParModule m)
{ return m.parFoldMap(documents,
                      new F<Stream<Character>, Long>()
                      { public Long f(final Stream<Character> document)
                        { return (long)fromStream(document).words().length(); }},
                      longAdditionMonoid)
          .claim(); }

要实例化 ParModule,您需要给它一个并行策略。 您可以实施自己的策略,或使用提供的策略。 这是使用 16 个线程的固定池的线程:

ExecutorService pool = newFixedThreadPool(16);
ParModule m = parModule(executorStrategy(pool));

对于上面的示例,您需要以下导入:

import fj.F;
import fj.data.Stream;
import fj.control.parallel.ParModule;
import static fj.control.parallel.ParModule.parModule;
import static fj.pre.Monoid.longAdditionMonoid;
import static fj.data.LazyString.fromStream;     
import static fj.control.parallel.Strategy.executorStrategy;
import static java.util.concurrent.Executors.newFixedThreadPool;
import java.util.concurrent.ExecutorService;

You should become familiar with the java.util.concurrent package. It has simple and robust primitives for parallel programming upon which to base higher-level libraries. One example of such a library is Functional Java, which has an easy-to-use module for parallel programming.

For example, here is the canonical MapReduce example written using Functional Java. It counts the number of words in a set of documents, assuming documents are Streams of characters:

public static long countWords(final List<Stream<Character>> documents,
                              final ParModule m)
{ return m.parFoldMap(documents,
                      new F<Stream<Character>, Long>()
                      { public Long f(final Stream<Character> document)
                        { return (long)fromStream(document).words().length(); }},
                      longAdditionMonoid)
          .claim(); }

To instantiate ParModule, you give it a parallel Strategy. You can implement your own Strategies, or use one that's supplied. Here's one that uses a fixed pool of 16 threads:

ExecutorService pool = newFixedThreadPool(16);
ParModule m = parModule(executorStrategy(pool));

You need the following imports for the example above:

import fj.F;
import fj.data.Stream;
import fj.control.parallel.ParModule;
import static fj.control.parallel.ParModule.parModule;
import static fj.pre.Monoid.longAdditionMonoid;
import static fj.data.LazyString.fromStream;     
import static fj.control.parallel.Strategy.executorStrategy;
import static java.util.concurrent.Executors.newFixedThreadPool;
import java.util.concurrent.ExecutorService;
为你鎻心 2024-08-01 15:24:35

java.util.concurrent 包自 java 5 起,但完全支持 java 7个功能

There are some limited support in java.util.concurrent package since java 5, but full supports are java 7 feature.

‘画卷フ 2024-08-01 15:24:35

你绝对应该尝试 Ateji Parralel 扩展。
http://www.ateji.com/multicore/

you should definetely try Ateji Parralel extensions.
http://www.ateji.com/multicore/

执手闯天涯 2024-08-01 15:24:35

这是 JSR166 工作组的网站,该工作组为 Java 7 编写了新的并发类。

http ://g.oswego.edu/dl/concurrency-interest/

列出了 2 个包:jsr166y(Java 7 的新并发类)和 extra166y(由于某种原因不会包含在内的各种类) )。

extra166y 中的类之一,ParallelArray 非常有用。 它自动并行化二分搜索、合并排序等。

Here is the site for the JSR166 working group, who wrote the new concurrency classes for Java 7.

http://g.oswego.edu/dl/concurrency-interest/

There are 2 packages listed: jsr166y (the new concurrency classes for Java 7) and extra166y (various classes that for one reason or another will not be included).

One of the classes in extra166y, ParallelArray is very useful. It automatically parallelizes binary searches, merge-sorts, etc.

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