在java中并行化任务的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2016083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the easiest way to parallelize a task in java?
提问by Eduardo
Say I have a task like:
假设我有一个类似的任务:
for(Object object: objects) {
Result result = compute(object);
list.add(result);
}
What is the easiest way to parallelize each compute() (assuming they are already parallelizable)?
并行化每个计算()的最简单方法是什么(假设它们已经可以并行化)?
I do not need an answer that matches strictly the code above, just a general answer. But if you need more info: my tasks are IO bound and this is for a Spring Web application and the tasks are going to be executed in a HTTP request.
我不需要与上面的代码严格匹配的答案,只需要一个一般性的答案。但是,如果您需要更多信息:我的任务是 IO 绑定的,这是针对 Spring Web 应用程序的,并且这些任务将在 HTTP 请求中执行。
采纳答案by overthink
I would recommend taking a look at ExecutorService.
我建议看看ExecutorService。
In particular, something like this:
特别是,像这样的事情:
ExecutorService EXEC = Executors.newCachedThreadPool();
List<Callable<Result>> tasks = new ArrayList<Callable<Result>>();
for (final Object object: objects) {
Callable<Result> c = new Callable<Result>() {
@Override
public Result call() throws Exception {
return compute(object);
}
};
tasks.add(c);
}
List<Future<Result>> results = EXEC.invokeAll(tasks);
Note that using newCachedThreadPool
could be bad if objects
is a big list. A cached thread pool could create a thread per task! You may want to use newFixedThreadPool(n)
where n is something reasonable (like the number of cores you have, assuming compute()
is CPU bound).
请注意,newCachedThreadPool
如果objects
列表很大,则使用可能会很糟糕。缓存线程池可以为每个任务创建一个线程!您可能想要使用newFixedThreadPool(n)
where n 是合理的(例如您拥有的核心数,假设compute()
受 CPU 限制)。
Here's full code that actually runs:
这是实际运行的完整代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ExecutorServiceExample {
private static final Random PRNG = new Random();
private static class Result {
private final int wait;
public Result(int code) {
this.wait = code;
}
}
public static Result compute(Object obj) throws InterruptedException {
int wait = PRNG.nextInt(3000);
Thread.sleep(wait);
return new Result(wait);
}
public static void main(String[] args) throws InterruptedException,
ExecutionException {
List<Object> objects = new ArrayList<Object>();
for (int i = 0; i < 100; i++) {
objects.add(new Object());
}
List<Callable<Result>> tasks = new ArrayList<Callable<Result>>();
for (final Object object : objects) {
Callable<Result> c = new Callable<Result>() {
@Override
public Result call() throws Exception {
return compute(object);
}
};
tasks.add(c);
}
ExecutorService exec = Executors.newCachedThreadPool();
// some other exectuors you could try to see the different behaviours
// ExecutorService exec = Executors.newFixedThreadPool(3);
// ExecutorService exec = Executors.newSingleThreadExecutor();
try {
long start = System.currentTimeMillis();
List<Future<Result>> results = exec.invokeAll(tasks);
int sum = 0;
for (Future<Result> fr : results) {
sum += fr.get().wait;
System.out.println(String.format("Task waited %d ms",
fr.get().wait));
}
long elapsed = System.currentTimeMillis() - start;
System.out.println(String.format("Elapsed time: %d ms", elapsed));
System.out.println(String.format("... but compute tasks waited for total of %d ms; speed-up of %.2fx", sum, sum / (elapsed * 1d)));
} finally {
exec.shutdown();
}
}
}
回答by David Rabinowitz
You can use the ThreadPoolExecutor. Here is sample code: http://programmingexamples.wikidot.com/threadpoolexecutor(too long to bring it here)
您可以使用ThreadPoolExecutor。这是示例代码:http: //programmingexamples.wikidot.com/threadpoolexecutor(太长了,无法将其带到这里)
回答by fastcodejava
One can simple create a few thread and get the result.
可以简单地创建几个线程并获得结果。
Thread t = new Mythread(object);
if (t.done()) {
// get result
// add result
}
EDIT : I think other solutions are cooler.
编辑:我认为其他解决方案更酷。
回答by mkamowski
I to was going to mention an executor class. Here is some example code that you would place in the executor class.
我要提到一个执行程序类。下面是一些示例代码,您将放置在 executor 类中。
private static ExecutorService threadLauncher = Executors.newFixedThreadPool(4);
private List<Callable<Object>> callableList = new ArrayList<Callable<Object>>();
public void addCallable(Callable<Object> callable) {
this.callableList.add(callable);
}
public void clearCallables(){
this.callableList.clear();
}
public void executeThreads(){
try {
threadLauncher.invokeAll(this.callableList);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object[] getResult() {
List<Future<Object>> resultList = null;
Object[] resultArray = null;
try {
resultList = threadLauncher.invokeAll(this.callableList);
resultArray = new Object[resultList.size()];
for (int i = 0; i < resultList.size(); i++) {
resultArray[i] = resultList.get(i).get();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultArray;
}
Then to use it you would make calls to the executor class to populate and execute it.
然后要使用它,您将调用 executor 类来填充和执行它。
executor.addCallable( some implementation of callable) // do this once for each task
Object[] results = executor.getResult();
回答by Adam Goode
For a more detailed answer, read Java Concurrency in Practiceand use java.util.concurrent.
有关更详细的答案,请阅读Java Concurrency in Practice并使用java.util.concurrent。
回答by Jonathan Feinberg
Here's something I use in my own projects:
这是我在自己的项目中使用的东西:
public class ParallelTasks
{
private final Collection<Runnable> tasks = new ArrayList<Runnable>();
public ParallelTasks()
{
}
public void add(final Runnable task)
{
tasks.add(task);
}
public void go() throws InterruptedException
{
final ExecutorService threads = Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors());
try
{
final CountDownLatch latch = new CountDownLatch(tasks.size());
for (final Runnable task : tasks)
threads.execute(new Runnable() {
public void run()
{
try
{
task.run();
}
finally
{
latch.countDown();
}
}
});
latch.await();
}
finally
{
threads.shutdown();
}
}
}
// ...
public static void main(final String[] args) throws Exception
{
ParallelTasks tasks = new ParallelTasks();
final Runnable waitOneSecond = new Runnable() {
public void run()
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}
};
tasks.add(waitOneSecond);
tasks.add(waitOneSecond);
tasks.add(waitOneSecond);
tasks.add(waitOneSecond);
final long start = System.currentTimeMillis();
tasks.go();
System.err.println(System.currentTimeMillis() - start);
}
Which prints a bit over 2000 on my dual-core box.
在我的双核盒子上打印了 2000 多点。
回答by i000174
With Java8 and later you can create a stream and then do the processing in parallel with parallelStream:
使用 Java8 及更高版本,您可以创建一个流,然后与parallelStream 并行处理:
List<T> objects = ...;
List<Result> result = objects.parallelStream().map(object -> {
return compute(object);
}).collect(Collectors.toList());
Note: the order of the results may not match the order of the objects in the list.
注意:结果的顺序可能与列表中对象的顺序不匹配。
Details how to setup the right number of threads are available in this stackoverflow question how-many-threads-are-spawned-in-parallelstream-in-java-8
在这个 stackoverflow question how-many-threads-are-spawned-in-parallelstream-in-java-8中提供了如何设置正确数量的线程的详细信息