java thread - Deadlock

Blocking problems to two or more threads in the implementation process due to competition resources caused by the absence of external force, they will not be able to promote it, then the system is in deadlock state.
Here Insert Picture Description

The reason Deadlock

  • A deadlock due to compete for resources
  • Order to promote the process deadlock improper occurred
    (there will be a deadlock on the database level)

A necessary condition for Deadlock:

  • Mutually exclusive conditions: resource use can only be one thread
  • Request to maintain conditions: a thread is blocked because the requested resource, the resource has been acquired to keep hold
  • Inalienable condition: thread has acquired a previously unused resources can not be arbitrarily deprived
  • Loop wait condition: forming a circle of head and tail connected between a plurality of threads waiting for a resource relationship
    Here Insert Picture Description

Deadlock prevention or released

Four necessary conditions for damage resulting deadlock: prevention (ex ante)

  • Destruction request holding conditions: one-time resource allocation
  • Undermine the inalienable condition: when the thread conditions are not met, freed resources have been occupied
  • Ordered allocation of resources: the system for each type of resource is assigned a number, and each thread sequentially numbered increasing resource request, released on the contrary

Avoid (in the matter): the banker's algorithm
allows threads to dynamically apply resources, the system first computing security resource allocation before allocation of resources, if the distribution does not cause the system to enter an unsafe state, the resources allocated to threads, otherwise, wait.

Detection and recovery (afterwards)
when the thread finds a deadlock, deadlock immediately accessible from out of state, by way of
deprivation of resources: from other threads deprive enough resources to deadlock the thread to lift the deadlock

Case deadlock

  • Producers and consumers incorrectly can produce a deadlock (wait, notify, notifyAll)
  • Multithreading obtain multiple locks, lock after the first release, after the release of the first lock
  • Dining philosophers problem

The actual position of the positioning deadlock ideas

1. First need to determine whether the java process deadlock
2. Open jvisualvm tool specifically analyze operational information JVMCPU, memory usage, as well as view the current status of the thread (color) java process individual threads running
3 or by a thread dump jvisualvm jstack command, the current call stack java process to print out all the threads of information
4. analyze the main thread and the child thread there is no key phrases:

  • waiting for (Resource Locator)
  • waiting to lock (Resource Locator)

5. Look thread function call stack, navigate to the source, to analyze specific issues

Guess you like

Origin blog.csdn.net/Alyson_jm/article/details/89599806