This is the 12th day of my participation in the August Challenge
Parallel worker concurrency model
The first type of concurrency model is the parallel worker concurrency model, where incoming work is assigned to different workers. The following is a flow chart of the parallel worker model.
- In the parallel worker concurrency model, the delegator distributes incoming work tasks to different workers, and each worker completes the complete task. Workers work in parallel on different threads and most likely on different cpus at the same time.
- If the parallel worker concurrency model is implemented in an automobile factory, each car is produced by one worker, and each worker will finish building a car from start to finish.
- Parallel workers are the most commonly used concurrency model in Java,
java.util.concurrent
Many of the concurrency utilities in the package are designed and implemented according to this model. - The concurrency model for parallel workers can be designed as shared state or separate state, which means that different threads can complete the sharing of certain resources, and there may be no shared state.
Advantages of the concurrent model for parallel workers
- The advantage of the parallel worker concurrency model is that it is easy to understand. To improve concurrency, add more workers.
- For example, for a web crawler application, you can set different minimum fetch times. Web crawlers are IO intensive, so the CPU has a lot of idle waiting time.
Disadvantages of the concurrent model for parallel workers
The parallel worker model also has some disadvantages under simple logic.
The shared state can become complex
- When shared objects need to access shared data in memory or a database, managing concurrent access becomes complicated. One part of the shared data is communication mechanisms such as message queues, while the other is business data, data caches, and connection pools.
- When parallel workers share data, the logic becomes very complex. One thread needs to make changes to data while ensuring that the data is visible to other threads (pushing it to main memory, not just staying in the CPU cache). Threads need to avoid race conditions, deadlocks, and other concurrency issues.
- In addition, when threads wait on each other, partial parallelization disappears and there is competition for shared data structures.
Stateless worker
- Shared variables can be modified by other threads, so the latest data must be read from main memory when used. Because of this stateless nature, reads are slow, especially when stored in external data.
Uncertain execution
- The disadvantage of the parallel worker domain is that the order of execution is uncertain. It is possible that job A is assigned before job B and executed after job B is executed. Because of this feature, the order of concurrent work is difficult to determine.
Afterword.
- How many events through the ages? Leisurely. The Yangtze River is still rolling.