——-Android Training, Java training, look forward to communicating with you! ———-
Deadlock one, basic concept
What is a deadlock?
Two threads hold one resource, respectively,
And at the same time request to get each other's resources.
In this case, the abnormal situation is called a deadlock.
Under what circumstances will a deadlock occur?
Deadlocks occur when synchronous nested synchronization is in progress.
There are at least 2 locks that will cause a deadlock.
Ii. Deadlock Example 1
First, show the deadlock with the example of a previous ticket.
class Ticket implements Runnable{ Private intTick = +; Object obj =NewObject ();BooleanFlag =true; Public voidRun () {if(flag) { while(true{synchronized (obj) {show (); } } }Else while(true) show (); } PublicSynchronizedvoidShow ()//this{synchronized (obj) {if(tick>0) {Try{Thread.Sleep (Ten);}Catch(Exception e) {} System.out.println (Thread.CurrentThread (). GetName () +".... Code:"+ tick--); } } }} classdeadlockdemo{ Public Static voidMain (string[] args) {Ticket T =NewTicket (); Thread T1 =NewThread (t); Thread t2 =NewThread (t); T1.start ();Try{Thread.Sleep (Ten);}Catch(Exception e) {} T.flag =false; T2.start (); }}
Running results See
, ticket for 1000, but sold to 984 when the card is not active,
Indicates that a deadlock has occurred.
Three, deadlock Example 2
Below I have written a brief example of a simple answer to test the deadlock.
//Create a lock class, note: Locks are objects, where objects of the object class are used class MyLock{ //Build two locks, note for easy invocation, use static modifier StaticObject Locka =NewObject ();StaticObject lockb =NewObject ();}//Create a class that will generate a deadlock class mydeadlock implements Runnable{ //Set an identity to allow two threads to execute a portion of their code Private BooleanFlag//using constructors to pass identitiesMydeadlock (BooleanFlag) { This. flag = Flag; }//Overwrite the Run method Public voidRun () {//If the identifier is true, the thread executes the if part code if(flag==true) {//Keep the program going until the deadlock while(true) {//Using synchronous code blocks to nest synchronization code fast manufacturing deadlocks //Because static modifies the lock, you can use the class name directly. Lock, call object directlySynchronized (Mylock.locka) {//Indicates that the thread has got the Locka lock in the IF statementSystem.out.println ("Thread:"+thread.currentthread (). GetName () +"Got the Locka in the IF. Lock ..."); Synchronized (MYLOCK.LOCKB) {//indicates that the thread got the LOCKB lock in the IF statementSystem.out.println ("Thread:"+thread.currentthread (). GetName () +"Got the lockb lock in If ..."); } } } }//Otherwise execute this part of the code of Else Else{//Keep the program going until the deadlock while(true) {//Using synchronous code blocks to nest synchronization code fast manufacturing deadlocksSynchronized (MYLOCK.LOCKB) {//indicates that the thread got the LOCKB lock in the Else statementSystem.out.println ("Thread:"+thread.currentthread (). GetName () +"I got the lockb lock in else ..."); Synchronized (Mylock.locka) {//indicates that the thread got the Locka lock in the Else statementSystem.out.println ("Thread:"+thread.currentthread (). GetName () +"I got the lockb lock in else ..."); } } } } }} class mydeadlockdemo { Public Static voidMain (string[] args) {//The thread object is established by thread class, and the object that implements the interface Runnable class is passed in as the actual parameter.Thread T1 =NewThread (NewMydeadlock (true)); Thread t2 =NewThread (NewMydeadlock (false));//Call Start method, start thread 1T1.start ();//In order to let the program run for a while and then deadlock Try{Thread.Sleep (Ten);}Catch(Exception e) {}//Call Start method, start thread 1T2.start (); }}
Run results
Thread: Thread-0 got locka. Lock in If.
Thread: Thread-0 got the lockb lock in If ...
Thread: Thread-0 got locka. Lock in If.
Thread: Thread-0 got the lockb lock in If ...
Thread: Thread-0 got locka. Lock in If.
Thread: Thread-0 got the lockb lock in If ...
Thread: Thread-0 got locka. Lock in If.
Thread: Thread-0 got the lockb lock in If ...
Thread: Thread-0 got locka. Lock in If.
Thread: Thread-1 got the lockb lock in else .....
The results show that when the thread Thread-1 just ran up, it immediately caused a deadlock
Iv. Summary
After understanding the principle of the deadlock, you must pay attention to the next procedure.
Avoid deadlocks, which can guarantee the security of the program.
Dark Horse Programmer _ Diary 16_java Multithreading (vi)