Controlling Task execution order with ExecutorService

前端 未结 8 1979
一生所求
一生所求 2020-12-02 14:30

I have a process which delegates asynch tasks to a pool of threads. I need to ensure that certain tasks are executed in order. So for example

Tasks arrive in order<

8条回答
  •  生来不讨喜
    2020-12-02 15:00

    In Habanero-Java library, there is a concept of data-driven tasks which can be used to express dependencies between tasks and avoid thread-blocking operations. Under the covers Habanero-Java library uses the JDKs ForkJoinPool (i.e. an ExecutorService).

    For example, your use case for tasks A1, A2, A3, ... could be expressed as follows:

    HjFuture a1 = future(() -> { doA1(); return true; });
    HjFuture a2 = futureAwait(a1, () -> { doA2(); return true; });
    HjFuture a3 = futureAwait(a2, () -> { doA3(); return true; });
    

    Note that a1, a2, and a3 are just references to objects of type HjFuture and can be maintained in your custom data structures to specify the dependencies as and when the tasks A2 and A3 come in at runtime.

    There are some tutorial slides available. You can find further documentation as javadoc, API summary and primers.

提交回复
热议问题