Java experiments on multi-threaded deadlock

The first complete code

Subsequent re-order modified block give the corresponding conclusions

1  // try to write to feel a deadlock 
2  public  class DeadLock {
 . 3  public  static  void main (String [] args) {
 . 4      Object O1 = new new Object ();
 . 5      Object O2 = new new Object ();
 . 6      // Create two a target object
 7      // Create thread two threads 
. 8      thread Tl = new new thread ( new new Process1 (O1, O2));    // two threads T1, T2 share the same two objects O1, O2 
. 9      thread T2 = new new the Thread ( new new Process2 (O1, O2));
 10     T1.setName ( "T1" );
 . 11      T2.setName ( "T2" );
 12 is      // Star RUN bit 
13 is      T1.start ();   // ---- T2 running
 14                    // ----- running T1 
15      T2.start (); 
 16      
. 17      
18 is  }
 . 19  }
 20 is  // first write a thread class. 
21 is  class Process1 the implements the Runnable {
 22 is      Object O1, O2;
 23 is      Process1 (Object O1, O2 Object) {
 24          the this .o1 = O1;
 25          the this .o2 = O2; 
26 is          }
 27      public  void RUN () {
 28          // first lock then lock o1 O2 
29          the synchronized (o1) {
 30              the try {
 31 is                  the Thread.sleep (1000);   // lock O1 and sleep lS 
32              } the catch (Exception E) {
 33 is                  e.printStackTrace ();
 34 is              }
 35              the synchronized (O2) { IF (.. Thread.currentThread () getName () the equals ( "t1")) { // T1 has successfully executed, and the return of the output o1 o2 two t1 the lock 
36          System.out.println ( "----- T1 running" );}
 37 [         if(Thread.currentThread().getName().equals("t2")){
38             System.out.println("----t2 running");}
39     }
40         }
41 }
42     }
43 
44 class process2 implements Runnable{
45     Object o1,o2;
46     process2(Object o1,Object o2 ){
47         this.o1 = o1 ;
48         this.o2 = o2 ; 
49         }
50     public void run(){
51         //先锁o2再锁o2 
52 is          the synchronized (O2) {   // lock O2 and sleep lS 
53 is              the try {
 54 is                  the Thread.sleep (1000 );  
 55              }
 56 is              the catch (Exception E) {
 57 is                  e.printStackTrace ();
 58              }
 59              the synchronized (O1) { 
 60                  IF (.. Thread.currentThread () getName () the equals ( "T1" )) {
 61 is          System.out.println ( "----- T1 running" );}
 62 is          IF . (Thread.currentThread () getName ( ) .equals ( "t2")) { //// after successful execution T2, t2 and return output o1 o2 two locks 
63             System.out.println("----t2 running");}
64     }
65             }
66 }
67     }

The key point of the experiment is to use Thread.sleep (); sleep on the threads, to achieve different lock and unlock the situation on the o1 o2.

Block A (run in T1 method):

RUN void public () { 
28 // first lock then lock o1 O2 
29 the synchronized (o1) { 
30 the try { 
31 is the Thread.sleep (1000); // lock O1 and sleep lS 
32} the catch (Exception E) { 
33 is E .printStackTrace (); 
34 is} 
after 35 synchronized (o2) {if ( .. Thread.currentThread () getName () equals ( "t1")) {// T1 successful implementation, t1 and return output o1 o2 two locks 
36 System.out.println ( "----- T1 running");} 
37 [IF (Thread.currentThread () getName () the equals ( "T2")..) { 
38 is System.out.println ( "--- running -T2 ");} 
39} 
40} 
41 is}

  

 

Code block B (run in method T2):

public void run(){
51         //先锁o2再锁o2 
52         synchronized (o2){  //锁住O2并睡1s
53             try{
54                 Thread.sleep(1000);  
55             }
56             catch(Exception e ){
57                 e.printStackTrace()  ;
58             }
59             synchronized(o1){ 
60                 if(Thread.currentThread().getName().equals("t1")){
61         System.out.println("-----t1 running");}
62 is          IF (Thread.currentThread (). GetName (). The equals ( "t2")) { //// After successful execution T2, t2 and return output o1 o2 two locks 
63 System.out.println ( "--- running -T2 " );}
 64}

Experiment 1: The blocks A and B Thread.sleep (1000); statement commented

Output Results: The probability Dading

-----t1 running
----t2 running

Small probability output

T2 running -----
---- T1 running such a result occurs because T1 easier access to the more CPU time block, all statements will be finished, and unlock o1 o2, only after the implementation of T2.star () statement, the output t2 running

Experiment 2: The block A Thread.sleep (1000); Zhushidiao

Output: the output will be a great chance

-----t1 running

After an interval of -------- lS:
---- running T2

Because of the statement executes T1.star ()

Thread T1 in the run statement is finished, thread T2 began to get CPU time slice, at this time o1 o2 unlocked. T2 is run blocks can be successfully performed (including sleep statement)

There will be a small chance of a deadlock: At this time T2 the first to receive CPU time slice, and locked o2, thread stops 1s, this time enough to run the statement o1 T1 will be locked, so o1 o2 lock can not be returned, formed a deadlock

 

Experiment 3: The block A and block B Thread.sleep (1000) have commented

Output:

 

Must not output, because whether T1 or T2 to get CPU time slice, a lock will be removed, and the thread is blocked 1s, this time is enough to pick up another lock another thread, then o1 o2 the locks can not be returned , the T1 T2 run statements were unable to continue down, the program is locked.

Guess you like

Origin www.cnblogs.com/Insertt/p/12117014.html