Java Concurrency Programming (i)-why concurrency

Source: Internet
Author: User
Tags cpu usage

The benefits of concurrency

1. concurrency can lead to performance gains in some cases (not all cases)

1) Increase the efficiency of CPU usage

Increase the utilization of multicore CPUs: In general, there will be more than one CPU core on a host, we can create multiple threads, in theory the operating system can assign multiple threads to different CPUs to execute, each CPU executes one thread, which improves the efficiency of CPU usage.

Increased CPU utilization When accessing I/O: When a thread is going to download something online, the thread will be in a blocking state, and the CPU will no longer be allocated CPU time for the thread, while other processes can gain CPU time without any impact. Conversely, if concurrency is not used, the entire process is suspended when the preceding instruction requests an I/O resource, even if the instruction after the CPU is idle is not executed.

2) Reduce the response time of the system

Imagine a server with 10,000 user access, if the use of single-threaded, all users of the request will go into a queue, if I happen to be the No. 9999 (starting from 0) users, then I have to wait until the previous 9,999 users of the request processing completed when the server will respond to me, My user experience will be very poor. If you use multithreading to avoid the problem of too long a response time, users can take turns using CPU resources, users may not always occupy the system, but regardless of the order of user access to the server, each user can quickly get the user's response.

3) Improve the fault tolerance of the system

A thread can run independently of the interference of other threads, and if a bug occurs in the code of a thread, the thread may throw an exception and the other thread can continue without any influence.

2. Easy to write code - - simulation

League of Legends This game should be heard by many people, this is a commercially successful 5v5 battle game, the game has a very "classic" game Mode: Man-machine battle. 5 computer-controlled heroes are implemented with at least 5 threads ( at least for rigor). 5 heroes seem to have their own ideas, the following is its approximate strategy pseudo-code:

Each thread executes its own policy, and they can also interact with each other by interacting between the threads. It's hard to use a single thread to implement a 5-hero strategy.

The drawbacks of concurrency

  Writing concurrent code is prone to error, and multithreading concurrency can cause a lot of uncertainty in execution, because only the same thread's internal code execution order is fixed, and the order of code execution between different threads cannot be determined. The problem occurs when multiple threads interfere with each other. When writing multithreaded code, if you do not consider the overall very easy to produce probabilistic, difficult to reproduce the bug.

System for thread context switching consumes a small amount of system resources, such as the side of the writing job while playing the phone, in the pick up the pen and pick up the operation of the phone needs to occupy the time, recall just do the problem when the idea also has taken up time, if first finished homework and then concentrate on playing mobile phone may achieve better results.

Summarize

Concurrency is not perfect, and whether or not to use concurrency should be weighed against the actual situation. With the increase of CPU core and the number of users of the system, multithreading is becoming more and more widely used. In most cases, the drawbacks of multithreading are insignificant in the face of their merits.

Java Concurrency Programming (i)-why concurrency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.