1.3.4 design concurrent and friendly applications
When using traditional command styles to Write multi-threaded applications, we have to deal with two problems:
■ It is difficult to convert the existing sequential code into parallel code, because to explicitly use the thread, you must modify most of the basic code.
■ It is difficult to use the shared status and lock. You must carefully consider how to use locks to avoid contention conditions and deadlocks, leaving enough space for parallel execution.
Function programming gives us the answer:
■ When using the Declarative Programming style, you can introduce parallelism in the existing code. With a few elements, you can combine commands with commands executed in parallel.
■ Because of its immutability, it is impossible to introduce contention conditions. You can write code that does not need to be locked. As long as you see which part of the program is independent, you can immediately change it to a parallel running task.
These two aspects can affect how we design applications, and make full use of multi-core machines by making it easier to write code that runs in parallel. However, writing immutable code is not just about getting merged rows. There is still some tricky work to do, but functional programming can minimize the extra effort spent on parallelization.
It's not over yet. When I start to think in the form of functions, I will find other changes in the design ......