Concurrent Programming Challenge: Deadlock and context switching


introduction

Context switching (also sometimes referred to as the process of switching or task switching) refers to the CPU switch from one process or thread to another process or thread. Context switch will affect the multi-threaded execution speed. Deadlock refers to multiple processes or threads loop waiting for a resource and its square occupied indefinitely stalemate situation.

1, a context switch

Context definition

When the cpu process or thread switch occurs, depends collection of data such as a function of external variables, function is running, must obtain external variables, the context is a set of values ​​for these variables.

Cause problems

For CPU-intensive tasks, multithreading context switch occurs, will affect the speed of execution, if when IO-intensive, multi-threading technology advantages of filling.

How to reduce context switching

  • Lock-free concurrent programming, lock acquisition and release of a context switch occurs, it will affect the efficiency of multi-threaded. Concurrent programming is to lock-free data block, and each thread the respective module. For example LongAdder portion of code.
  • CAS algorithm, the update data through the CAS algorithm concurrent programming without having to lock. The tools in the atomic Java packages.
  • With a minimum thread, reduce unnecessary thread creation, a custom thread pool.
  • Use coroutine, maintenance scheduling multiple tasks in a single thread, switching between processing tasks, Golang Association process for the use of very powerful.

2, deadlock

Deadlock is defined

Deadlock Deadlock is the process short, is first proposed by the study in 1965 by the banker's algorithm Dijkstra.
System deadlock occurrence not only waste a lot of system resources, and even lead to the collapse of the entire system, with disastrous consequences.
死锁.png

The reason deadlock

  • Insufficient system resources
  • Improper order to promote the process
  • Irrational distribution of resources

A necessary condition for Deadlock

  • Mutually exclusive conditions: a resource can only be a process or thread to use.
  • Request and keeping conditions: a process or thread, blocking occurs when the requested resource, a resource that has been acquired to keep hold.
  • Inalienable conditions: process or thread in order to obtain the resources, upon completion is not used, it can not be forcibly deprived.
  • Circular wait condition: a number of processes or threads to form a relationship of end to end resource cycle wait.

This four conditions are necessary conditions for deadlock, as long deadlock occurs, these conditions are bound to set up, as long as one of the above conditions are not met, it will not happen deadlock.

How to avoid deadlocks

  1. In order to obtain the determined lock
  2. 加锁时限
    Lock接口提供了boolean tryLock(long time, TimeUnit unit) throws InterruptedException方法,该方法可以按照固定时长等待锁,因此线程可以在获取锁超时以后,主动释放之前已经获得的所有的锁。
  3. 死锁检测(银行家算法)

tencent.jpg

Guess you like

Origin www.cnblogs.com/clawhub/p/11978506.html