如何使用多个线程并行运行以执行非常繁重的任务 [英] How to use several threads to run in parallel to execute a very heavy task
问题描述
我需要执行一个包含四个步骤的任务. 每个步骤都取决于上一个步骤的结果.在一个线程中执行所有步骤需要很长时间. 我想使用四个线程,每个线程执行一个步骤,并在相邻的两个步骤之间使用一个缓冲区来存储上一步的结果. 我正在使用Java在Android平台上进行开发. 有人可以给我一个例子吗?
I need to execute a task which includes four steps. Each step relies on the result of previous one. Executing all steps in one thread needs long time. I want to use four threads and each one performs one step and use a buffer between neighboring two steps to store the result of the previous step. I am developing on Android platform using Java. Can anybody give me an example?
非常感谢.
YL
推荐答案
我很好奇,(非神奇并行)代码在java9中使用响应流的样子.原来,Java9基础架构是零碎的,并且 JavaRx ,以提供流".有一个 android扩展.
I was curious, how the (non magically parallel) code would look like using reactive streams in java9. Turned out, the Java9 infrastructure is fragmentary and to be used with caution. So I chose to base the example on JavaRx, providing "Flows". There's an android extension available for that.
我将其与Streams,parallelStreams和顺序流结合在一起.
I put it in perspective with Streams, parallelStreams and sequential flows.
public class FlowStream {
@Test
public void flowStream() {
int items = 10;
List<Integer> source = IntStream.range(0, items - 1).boxed().collect(Collectors.toList());
print("\nstream");
source.stream().map(this::exp).map(this::exp).forEach(i -> print("streamed %d", i));
print("\nparallelStream");
source.parallelStream().map(this::exp).map(this::exp).forEach(i -> print("streamed %d parallel", i));
print("\nflow");
Flowable.range(0, items)
.map(this::exp)
.map(this::exp)
.forEach(i -> print("flowed %d", i));
print("\nparallel flow");
Flowable.range(0, items)
.flatMap(v ->
Flowable.just(v)
.subscribeOn(Schedulers.computation())
.map(this::exp)
)
.flatMap(v ->
Flowable.just(v)
.subscribeOn(Schedulers.computation())
.map(this::exp)
).forEach(i -> print("flowed parallel %d", i));
await(5000);
}
private Integer exp(Integer i) {
print("making %d more expensive", i);
await(Math.round(10f / (Math.abs(i) + 1)) * 50);
return i;
}
private void await(int i) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private void print(String pattern, Object... values) {
System.out.println(String.format(pattern, values));
}
}
<!-- https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxjava -->
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.2.13</version>
</dependency>
这篇关于如何使用多个线程并行运行以执行非常繁重的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!