How to use several threads to run in parallel to execute a very heavy task

為{幸葍}努か 提交于 2020-01-25 10:09:45

问题


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


回答1:


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<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>


来源:https://stackoverflow.com/questions/58444801/how-to-use-several-threads-to-run-in-parallel-to-execute-a-very-heavy-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!