One, deadlockThe so-called deadlock: refers to two or more than two processes in the process of running. A waiting phenomenon caused by contention for resources. If there is no external force. They will not be able to go forward.
Encyclopedia Encyclopedia
When more than two computing units, both sides are waiting for the other side to stop execution, to obtain system resources, but no one side early exit, such a situation, is called a deadlock. Wikipedia
1. Sequential deadlocks have a minimum of two locks. A thread acquires a lock to get the B lock talent to operate, while another thread acquires a B lock. Need to get a lock talent run operation. In such cases easy appears in sequential deadlock.
public class Leftrightdeadlock {Private Final object left = new Object ();p rivate Final Object right = new Object ();p ublic void LeftRight () {synchronized (left) {synchronized (right) {//DoSomething ()}}} public void Rightleft () {synchronized (right) {synchronized (left) {//DoSomething ()}}}}
2. Dynamic Lock sequence deadlock
public void TransferMoney (account Fromaccount, account Toaccount, Dollaramount anount) throws insufficientresourcesexception {synchronized (fromaccount) {synchronized (toaccount) {if (Fromaccount.getbalance (). CompareTo (amount) < 0) {throw new Insufficientresourcesexception ();} else {fromaccount.debit (anount); Toaccount.credit (Anount);}}}
A:transfermoney (MyAccount, Youraccount, 10);
B:transfermoney (Youraccount, MyAccount, 20);
The condition of all locks by the externally passed-in variables, however, can be seen by the variables passed in, in which case a thread acquires a myaccount lock in the request Youraccount lock, while another thread acquires the Youraccount lock on the opposite side of the request MyAccount lock.
Private static Final Object Tielock = new Object ();p ublic void TransferMoney (Final account Fromaccount, final account TOAC Count, final Dollaramount anount) throws Insufficientresourcesexception {class helper{public void Transfer () throws Ins ufficientresourcesexception {if (Fromaccount.getbalance (). CompareTo (amount) < 0) {throw new insufficient Resourcesexception (); } else{fromaccount.debit (Anount); Toaccount.credit (Anount); }}}int Fromhash = System.identityhashcode (fromaccount); int tohash = System.identityhashcode (ToAccount); if (FromHash &l T Tohash) {synchronized (Fromaccount) {synchronized (toaccount) {new Helper (). Transfer (); }}} and else if (Fromhash > Tohash) {synchronized (Toaccount) {synchronized (fromaccount) {new H Elper (). Transfer (); }}} else {synchronized (Tielock) {synchronized (Fromaccount) {synchronized (toaccount) { New Helper (). TraNsfer (); } } }}}
3. Deadlocks that occur between collaboration objects
Class Taxi {Private point location, destination;private final Dispatcher dispatcher;public Taxi (Dispatcher Dispatcher) { C0/>this.dispatcher = Dispatcher;} Public synchronized Point GetLocation () { return location;} Public synchronized void setlocation { this.location = location; if (location.equals (destination)) { dispatcher.notifyavaliable (this);}} } class Dispatcher {private Final Set <Taxi> taxis;private Final set<taxi> avaliabletaxis;public Dispatcher () { taxis = new Hashset<taxi > (); Avaliabletaxis = new hashset<taxi> ();} Public synchronized void notifyavaliable (Taxi Taxi) { avaliabletaxis.add (Taxi);} Public synchronized Image GetImage () { image image = new Image (); for (Taxi t:taxis) { image.drawmarker (t.getlocation ()); } return image;}}
4. Open call--to be filled
5. Resource Deadlockexternal locks are often overlooked and lead to deadlocks, such as database locks
Two, the avoidance of the deadlock with the diagnosis 1. Support for timed deadlocksThere are some means of preventing deadlocks. For example, lock TRYLOCK,JDK 7 introduced in the Phaser and so on.
2. Parsing deadlocks with thread dump informationthrough the stacktrace of the dump thread, such as running command kill-3 <pid> under Linux, or Jstack–l <pid>, or using Jconsole connection to view the stacktrace of the thread, This is to diagnose the deadlock problem.
Third, the other active critical 1. Hunger 2. Poor responsiveness of 3. Live lock
Iv. the use of locks uses a data structure that supports CAs. Avoid using locks. such as: Atomicxxx, Concurrentmap, Copyonwritelist, Concurrentlinkedqueue deadlock is often not completely avoided, ostrich strategy is used by a very many basic framework.
There is a way to detect a deadlock
V. References:"Win Shaojin-java Concurrency Programming tutorial"
The tenth chapter of Java concurrent programming to avoid active critical reading notes