Java Concurrent Programming (1) - Why Concurrency

The benefits of concurrency

1. Concurrency can bring performance improvements in some cases (not all cases)

1) Improve the efficiency of CPU usage

  Improve the utilization of multi-core CPU: Generally speaking, there will be multiple CPU cores on a host, and we can create multiple threads. In theory, the operating system can assign multiple threads to different CPUs for execution. Each CPU Execute a thread, which improves the efficiency of CPU usage.

  Improve CPU utilization when accessing I/O: When a thread wants to download something on the Internet, the thread will be in a blocked state, and the CPU will no longer allocate CPU time for this thread, and other processes can not Get CPU time without any impact. Conversely, if concurrency is not used, when the previous instruction applies for I/O resources, the entire process is suspended, and subsequent instructions cannot be executed even if the CPU is in an idle state.

2) Reduce the response time of the system

   Suppose a server is accessed by 10,000 users at the same time. If a single thread is used, all user requests will be queued in a queue. If I happen to be the 9999th (counting from 0) user, then I have to wait until the front The server will respond to me only when the requests of 9999 users have been processed, and my user experience will be very poor. If multi-threading is used, the problem of excessive response time can be avoided. Users can take turns using CPU resources. Users may not always occupy the system, but regardless of the order in which users access the server, each user can quickly get a response from the user.

3) Improve the fault tolerance of the system

  A thread can run independently without the interference of other threads. If there is a bug in the code of a thread, the thread may throw an exception and exit. At this time, other threads can continue to execute without any influence.

2. Easy to write code - simulation

  Many people have heard of the game League of Legends. This is a commercially successful 5V5 battle game. This game has a very "classic" game mode: human-machine battle. 5 computer-controlled heroes are implemented with at least ( at least for the sake of rigor) 5 threads. The 5 heroes seem to have their own ideas. The following is its approximate strategy pseudocode:

 

  Each thread executes its own strategy, and they can also interact with each other to realize the skill cooperation between heroes. If you use a single thread to implement a strategy of 5 heroes, it is extremely difficult.

Disadvantages of concurrency

  Writing concurrent code is prone to errors, and multi-thread concurrent operation brings a lot of uncertainty to the execution process, because only the execution order of code within the same thread is fixed, and the execution order of code between different threads cannot be determined. Problems arise when multiple threads interfere with each other. When writing multi-threaded code, it is easy to generate probabilistic and difficult-to-reproduce bugs without comprehensive consideration.

  When the system performs thread context switching, a small amount of system resources will be consumed. For example, when you are writing your homework and playing with your mobile phone, the operations of picking up the pen and picking up the mobile phone will take up time. Recalling the idea of ​​​​the question just now also takes up time. If You may get better results after finishing your homework first and then concentrating on playing with your phone.

Summarize

  Concurrency is not perfect, and whether to use concurrency should also weigh the pros and cons of the actual situation. With the increase in the number of CPU cores and users of the system, the application of multithreading is becoming more and more widespread. In most cases, the disadvantages of multithreading outweigh the advantages.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325092642&siteId=291194637