在java中有没有一种简单的方法来并行化foreach循环?
在Java8中,有没有一种简单的方法可以使用一些库的东西来并行化foreach循环在java中有没有一种简单的方法来并行化foreach循环?,java,multithreading,foreach,Java,Multithreading,Foreach,在Java8中,有没有一种简单的方法可以使用一些库的东西来并行化foreach循环 void someFunction(SomeType stuff, SomeType andStuff) { for (Object object : lotsOfObjects) object.doSomethingThatCanBeDoneInParallel(stuff, andStuff); } 多线程处理有点痛苦和耗时,所以我想知道是否有更简单的方法使用一些库来完成上述操作 谢
void someFunction(SomeType stuff, SomeType andStuff) {
for (Object object : lotsOfObjects)
object.doSomethingThatCanBeDoneInParallel(stuff, andStuff);
}
多线程处理有点痛苦和耗时,所以我想知道是否有更简单的方法使用一些库来完成上述操作
谢谢
2018年6月3日编辑
ExecutorServices确实非常方便,我不能使用shutdown()等待,因为我每帧运行一次,并且每帧创建一个新的ExecutorServices会太贵
最后我写了一个类来并行化fori循环,我想我可以和其他像我这样的新手分享它
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
public class ParallelForI {
public ParallelForI(int numberOfThread) {
NUMBER_OF_THREAD = numberOfThread;
executorService = Executors.newFixedThreadPool(NUMBER_OF_THREAD);
finished = new AtomicBoolean[NUMBER_OF_THREAD];
for (int i = 0; i < finished.length; i++)
finished[i] = new AtomicBoolean(true);
// true is better for waitForLastRun before any run.
}
private ExecutorService executorService;
private final int NUMBER_OF_THREAD;
private AtomicBoolean[] finished;
public void waitForLastRun() {
synchronized (this) {
/* synchronized outside the loop so other thread
can't notify when it's not waiting. */
for (int i = 0; i < NUMBER_OF_THREAD; i++) {
if (!finished[i].get()) {
i = -1;
try {
this.wait(); //
} catch (InterruptedException e) {
// do nothing and move one.
}
}
}
}
}
public void run(FunctionForI functionForI, final int MAX_I) {
for (AtomicBoolean finished : finished)
finished.set(false); // just started
for (int i = 0; i < NUMBER_OF_THREAD; i++) {
final int threadNumber = i;
executorService.submit(new Runnable() {
@Override // use lambda if you have java 8 or above
public void run() {
int iInitial = threadNumber * MAX_I / NUMBER_OF_THREAD;
int iSmallerThan;
if (threadNumber == NUMBER_OF_THREAD - 1) // last thread
iSmallerThan = MAX_I;
else
iSmallerThan = (threadNumber + 1) * MAX_I / NUMBER_OF_THREAD;
for (int i1 = iInitial; i1 < iSmallerThan; i1++) {
functionForI.run(i1);
}
finished[threadNumber].set(true);
synchronized (this) {
this.notify();
}
}
});
}
}
public interface FunctionForI {
void run(int i);
}
}
使用并行流。但这不是一个通用的解决方案。使用并行流。但这不是一个通用的解决方案。解决方案可以是在
线程中启动每个任务,如下所示:
new Thread(() -> object.doSomethingThatCanBeDoneInParallel(stuff, andStuff)).start();
但是这不是一个相关的解决方案,因为创建线程的成本很高,所以有一些机制和工具可以帮助您:Executors
类来构建一些池
一旦您拥有了将管理此任务的实例,就可以为其提供任务,这些任务将在您选择的线程数上并行运行:
void someFunction(SomeType stuff, SomeType andStuff) {
ExecutorService exe = Executors.newFixedThreadPool(4); // 4 can be changed of course
for (Object object : lotsOfObjects) {
exe.submit(() -> object.doSomethingThatCanBeDoneInParallel(stuff, andStuff));
}
// Following lines are optional, depending if you need to wait until all tasks are finished or not
exe.shutdown();
try {
exe.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
解决方案可以是在线程中启动每个任务,如下所示:
new Thread(() -> object.doSomethingThatCanBeDoneInParallel(stuff, andStuff)).start();
但是这不是一个相关的解决方案,因为创建线程的成本很高,所以有一些机制和工具可以帮助您:Executors
类来构建一些池
一旦您拥有了将管理此任务的实例,就可以为其提供任务,这些任务将在您选择的线程数上并行运行:
void someFunction(SomeType stuff, SomeType andStuff) {
ExecutorService exe = Executors.newFixedThreadPool(4); // 4 can be changed of course
for (Object object : lotsOfObjects) {
exe.submit(() -> object.doSomethingThatCanBeDoneInParallel(stuff, andStuff));
}
// Following lines are optional, depending if you need to wait until all tasks are finished or not
exe.shutdown();
try {
exe.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
java中的For循环本质上是串行的。对他们没有办法做这种事。不过,通过引入流,您可以使用它们并行化集合上的操作 java中的For循环本质上是串行的。对他们没有办法做这种事。不过,通过引入流,您可以使用它们并行化集合上的操作 我爱这个社区我爱这个社区同意复制的想法,但答案是2014年的,可以更新^^同意复制的想法,但答案是2014年的,可以更新^^