The main causes of deadlocks are:
(1) Due to insufficient system resources.
(2) The sequence of progress of the process is not appropriate.
(3) Improper allocation of resources and so on.
If the system resources are sufficient, the resource request of the process can be satisfied, the probability of deadlock is very low, otherwise
will be locked into a deadlock over a limited resource. Second, the process is run in a different order and speed, and may also produce a deadlock.
The four necessary conditions for creating a deadlock:
(1) Mutex condition : A resource can only be used by one process at a time.
(2) request and hold condition : When a process is blocked by a request for resources, it remains in place for the resources that have been obtained.
(3) Conditions of deprivation : the resources that the process has acquired cannot be forcibly deprived of until the end of its use.
(4) cyclic waiting condition : A cyclic waiting resource relationship is formed between several processes.
These four conditions are necessary for the deadlock, as long as the system has a deadlock, these conditions must be established, and as long as the above conditions
If not satisfied, there will be no deadlock.
Release and prevention of deadlocks:
Understanding the causes of deadlocks, especially the four necessary conditions that generate deadlocks, can be avoided, prevented, and
Unlock the deadlock. Therefore, in the system design, process scheduling and other aspects of how to not let these four necessary conditions to set up, how to accurately
The rational allocation of resources to avoid the process of permanent occupation of system resources. Also, prevent the process from being in a waiting state
Resource usage. Therefore, the allocation of resources should be given reasonable planning.
Here is the use of Java to deliberately implement a deadlock, which is also one of the common face questions:
PackageDead lock; Class a{synchronized voidFuna (b b) {String name = Thread.CurrentThread (). GetName (); System.out.println (name+"Enter A.foo");/*try {thread.sleep (2000); } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); }*/System.out.println (name+"Call the last method of Class B"); B.last (); }synchronized voidLast () {System.out.println ("A's Last Method"); }}class b{synchronized voidFunb (a A) {String name = Thread.CurrentThread (). GetName (); System.out.println (name+"Enter B.foo");Try{Thread.Sleep ( -); }Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); } System.out.println (name+"Call the last method of Class A"); A.last (); }synchronized voidLast () {System.out.println ("The Last method of B"); }} Public class Deadlockdemo implements Runnable{A a=NewA (); B b=NewB (); Public Deadlockdemo() {//TODO auto-generated constructor stub NewThread ( This). Start (); A.funa (b); } Public void Run() {B.funb (a); } Public Static void Main(String []args) {NewDeadlockdemo (); }}
About deadlocks, using Java to implement a deadlock