The only remaining difference between Fork/Join and splitting the task upfront is this: When splitting upfront you have the work queue full right from start. Example: 1000 units, the threshold is 10, so the queue has 100 entries. These packets are distributed to the threadpool members.
Fork/Join is more complex and tries to keep the number of packets in the queue smaller:
- Step 1: Put one packet containing (1...1000) into queue
- Step 2: One worker pops the packet(1...1000) and replaces it with two packets: (1...500) and (501...1000).
- Step 3: One worker pops packet (500...1000) and pushes (500...750) and (751...1000).
- Step n: The stack contains these packets: (1..500), (500...750), (750...875)... (991..1000)
- Step n+1: Packet (991..1000) is popped and executed
- Step n+2: Packet (981..990) is popped and executed
- Step n+3: Packet (961..980) is popped and split into (961...970) and (971..980). ....
You see: in Fork/Join the queue is smaller (6 in the example) and the "split" and "work" phases are interleaved.
When multiple workers are popping and pushing simultaneously the interactions are not so clear of course.
Read full article from java - How is the fork/join framework better than a thread pool? - Stack Overflow
No comments:
Post a Comment