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?
Thanks a lot.
YL
解决方案
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.
I put it in perspective with Streams, parallelStreams and sequential flows.
public class FlowStream {
@Test
public void flowStream() {
int items = 10;
List 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));
}
}
io.reactivex.rxjava2
rxjava
2.2.13