Java - splitting work to multiple threads

前端 未结 6 1977
谎友^
谎友^ 2020-12-14 12:07

I am posed with the following problem: I need to split work across multiple threads for perfomance reasons, but I am not sure what approach to take.

Firstly, the tas

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 12:29

    Slightly different approach is:

    • create a LinkedBlockingQueue

    • pass it to each task. Tasks can be Threads, or Runnables upon j.u.c.Executor.

    • each task adds its result to the queue

    • the main thread reads results using queue.take() in a loop

    This way results are handled as soon as they are computed.

提交回复
热议问题