gpt4 book ai didi

多核上的 Java 并行执行

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:38 25 4
gpt4 key购买 nike

我想知道是否存在像 C# 和 Linq 中那样在 Java 中并行化查询的方法(或者有框架或库):

var query = from item in source.AsParallel().WithDegreeOfParallelism(2)
where Compute(item) > 42
select item;

如果我不能并行化查询,如果我可以在 C# 中做这样的事情(每个并行化):

  Parallel.ForEach(files, currentFile =>
{
// The more computational work you do here, the greater
// the speedup compared to a sequential foreach loop.
string filename = System.IO.Path.GetFileName(currentFile);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(currentFile);

bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
bitmap.Save(System.IO.Path.Combine(newDir, filename));

// Peek behind the scenes to see how work is parallelized.
// But be aware: Thread contention for the Console slows down parallel loops!!!
Console.WriteLine("Processing {0} on thread {1}", filename,
Thread.CurrentThread.ManagedThreadId);

}

如果您发布了任何框架或库,能否告诉我您使用它的经验?谢谢你的时间。

关于 c# 和 linq,您可以在此处找到文档:http://msdn.microsoft.com/en-us/library/dd997425.aspx

最佳答案

没有直接翻译。首先,Java 没有 LINQ,也没有任何标准的并行集合类。

GPars可能是最合适的。

请注意,虽然它是针对 groovy 的,但它的 API 完全可以从 java 中使用

关于多核上的 Java 并行执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12874472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com