java concurrent programming - Chapter concurrent programming challenges

A, java concurrent programming challenges

Concurrent programming problems that need attention:

Concurrent programming goal is to make the program run faster, but not to start more threads can make the program maximum concurrent execution. If you want to make the concurrent program execution faster, be subject to the following issues challenge by multithreading:

The thread context switching problem, deadlock, limited by the hardware and software resources.

1, thread context switching

Thread context switching DESCRIPTION: CPU cycles to perform the task time slice allocation algorithm, each thread is a task under normal circumstances be executed by the CPU polling the current task execution thread will switch to the next task after the completion of a time slice. When the switch is saved before the execution state of a task, and then cut back for the next task, know where to step task execution. Context switch from one task is to save the process again loaded.

Timely also supports multiple threads of execution in single-core processor, CPU time slice to each thread to achieve concurrency. CPU time slice is assigned a thread of execution time, CPU time slice is very short (tens of milliseconds), so the CPU need to keep switching threads, giving us the impression that multiple threads execute simultaneously.

Every time a context switch is required consumption of resources, thus affecting the efficiency of the implementation of multi-threading. Similar to a person while in the dry multi-thing, everything is a little on the dry and do something else, and when the switch must be recorded at the nodes of the current task at hand. So the more things he made, the higher the cost of context switching, and lower the overall efficiency.

The core idea is to reduce context switching: minimize the number of threads, reducing the use of locks as much as possible;

Lock-free concurrent programming: cause a context switch, avoid sharing resources is multi-threaded competitive scene as a thread lock contention. Modulo hash task segment may be employed, each different thread processing job data.

CAS algorithm: Atomic java package in comparison to use the underlying code and replace (CAS) algorithm to solve shared resource competition and avoid the use of locks.

Minimum number of threads: avoid creating too many threads, resulting in a large number of threads in an idle state. Resulting in less task, thread and more scenes, such a large number of threads in a wait state.

Coroutine: as the number of tasks into a thread segments to achieve and maintain switching between multiple tasks in a single thread.

Former two ways to avoid using locks, after two ways is to minimize the number of threads.

2, deadlock

Deadlock Description: two threads that hold each other needed resources, and while they wait for each other to release resources, lead to stalemate each other two threads, who also refused to who.

We do not usually write such code sucker, but may occur in actual operations, the lock is released when throwing an exception that led to the release of the lock is not normal, or abnormal condition because the lock is not locked successfully released and so on.

Ways to avoid deadlock:

Avoid a thread simultaneously acquire multiple locks;

Avoid a thread holds multiple resources simultaneously in the lock, usually a lock of a lock resource.

Try using time lock, the lock is automatically released after exceed the effective time.

3, resource constraints challenge

Description: In concurrent programming, subject to hardware limitations or software resources, resulting in execution speed of the program affected.

The limitations of the hardware and software resources is flawed, since there is no anti-aircraft guns, not fight aircraft, concurrent programming on the premise that the hardware and software must have a certain standard. Solutions to upgrade is to upgrade the hardware and software.

 

Guess you like

Origin www.cnblogs.com/aoshicangqiong/p/11442350.html