The special type of error that needs to be avoided in relation to multitasking is deadlock (deadlock). A deadlock occurs when two threads have a circular dependency on a pair of synchronization objects. For example, suppose one thread enters the pipe of object x and another thread enters the enhancement of object Y. If X's thread tries to invoke the synchronous method of Y, it will be locked as expected. The thread of Y also wants to invoke some of the synchronization methods of x, and the thread waits forever, because in order to reach X, you must release the lock of your own y so that the first thread can finish. Deadlock is a difficult error to debug because:
- Typically, it rarely occurs, only if the time period of the two threads coincides.
- It may contain more than two threads and synchronization objects (that is, deadlocks can occur when there are more complex sequence of events than the example just described).
To fully understand the deadlock, it is useful to observe its behavior. The following example generates two classes, A and B, each with the Foo () and Bar () methods. Both methods have a short pause before invoking the methods of other classes. The main class, named Deadlock, creates an instance of a and B, and then initiates a second thread to set the deadlock environment. The foo () and Bar () methods use Sleep () to force deadlocks to occur.
1 //An example of deadlock.2 classA {3 synchronized voidFoo (b b) {4String name =Thread.CurrentThread (). GetName ();5SYSTEM.OUT.PRINTLN (name + "entered A.foo");6 Try {7Thread.Sleep (1000);8}Catch(Exception e) {9System.out.println ("A interrupted");Ten } OneSYSTEM.OUT.PRINTLN (name + "trying to call B.last ()"); A b.last (); - } - synchronized voidLast () { theSystem.out.println ("Inside a.last"); - } - } - classB { + synchronized voidBar (a a) { -String name =Thread.CurrentThread (). GetName (); +SYSTEM.OUT.PRINTLN (name + "entered B.bar"); A Try { atThread.Sleep (1000); -}Catch(Exception e) { -System.out.println ("B interrupted"); - } -SYSTEM.OUT.PRINTLN (name + "trying to call A.last ()"); - a.last (); in } - synchronized voidLast () { toSystem.out.println ("Inside a.last"); + } - } the classDeadlockImplementsRunnable { *A A =NewA (); $b b =NewB ();Panax Notoginseng Deadlock () { -Thread.CurrentThread (). SetName ("Mainthread"); theThread T =NewThread ( This, "Racingthread"); + T.start (); AA.foo (b);//get lock on a the this thread. theSystem.out.println ("Back in Main thread"); + } - Public voidrun () { $B.bar (a);//get lock on B in the other thread. $System.out.println ("Back on other thread"); - } - Public Static voidMain (String args[]) { the NewDeadlock (); - }Wuyi}
After running the program, the output is as follows:
Mainthread entered A.foo
Racingthread entered B.bar
Mainthread trying to call B.last ()
Racingthread trying to call A.last ()
Because the program is deadlocked, you need to press CTRL-C to end the program. On the PC Press Ctrl-break (or press ctrl-\ under Solaris) you can see the full range and the pipe buffer heap. You will see that racingthread occupies pipe b while waiting for pipe A, while Mainthread occupies a waits for B. The program will never end. As this example illustrates, your multithreaded routines are often locked, and deadlocks are the first thing you should check.
Series Articles:
Java know how much (top)
Java know how much (interface) interface
Java knows how much (40) the difference between an interface and an abstract class
Java know how much (41) generic explanation
Java know how much (42) the range of generic wildcard characters and type parameters
Java know how much (43) Exception Handling Basics
Java know how much (44) exception type
Java know how much (45) uncaught exceptions
How much Java knows (the) use of try and catch
Java know how much (47) use of multiple catch statements
Java knows how much (in) the nesting of Try statements
Java know how much (a) throw: Exception throws
Java know how many () Java throws clauses
Java knows how many (or) finally
Java know how much (52) built-in exceptions
Java know how much (53) Use Java to create your own exception subclasses
Java know how much (54) assertion detailed
Java know how many (55) threads
Java know how much (56) threading ModelJava know how much (57) main threadJava know how much (58) thread Runnable interface and thread class explanationJava know how much (59) Create multithreadingJava know how much (isAlive) and join () useJava know how much (61) thread PriorityJava know how much (62) thread synchronizationJava know how much (63) inter-thread communication
Java know how much (64) thread deadlock