A question about thread locks

Mashi Bing thread synchronization video mentioned in the question, is a method of locking and locking the two methods of distinction, look at the following case, the method lock m1, m2 unlocked, tt.b in print and m1 b values ​​are how much?

public  class TT the implements the Runnable {
     int B = 100 ; 

    public  the synchronized  void M1 () throws Exception { 
        System.out.println ( "begin M1 --- Step" ); 
        B = 1000 ;
         the Thread.sleep ( 5000 ); 
     System.out.println ( "M1 --- begin the second step"); System.out.println (
"B =" B +); } public void M2 () throws Exception { System.out.println ( "start The first step in the implementation m2 --- " ); Thread.sleep ( 2500); b = 2000; System.out.println("开始执行m2---第二步"); } public void run() { try { m1(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { TT tt = new TT(); Thread t = new Thread(tt); t.start(); tt.m2(); System.out.println(tt.b); } }

Script execution result is 2000, why is it so? My understanding is that tt is an instance of an object class TT thread, the equivalent of tt is the main thread to perform, while the child thread t is a new thread created on the basis of tt, create a sub-thread takes time, so the main thread tt.m2 () first execution, wait until tt.m2 () to perform the first step, the beginning of sleep 2500ms, this time threading position to grab the quilt thread t has passed, the child thread t do the first step, and then change the value of b 1000, this time began to sleep 5000ms, this time they cut back to the main thread m2, m2 will become 2000 b, the second step, and then the main thread print tt.b = 2000, the main thread of execution is over, cut back child thread t, t the child thread executed the second step, and then print b = 2000

Begin m2 --- first step
begin m1 --- first step
begin m2 --- Step
2000
started m1 --- Step
b = 2000

In another case, that m1 and m2 are locked, the order of execution are the main thread tt.m2 () -> m2 first step -> Sleep 2500ms -> b = 2000 -> m2 Step Two - -> Print tt.b = 2000, then switch back to m1, m1 first step -> b = 1000 -> sleep 5000ms -> m1 Step two -> b = 1000, that is, two methods are added lock, a first thread executed, then the other threads of execution method

public  class TT2 the implements the Runnable {
     int B = 100 ; 

    public  the synchronized  void M1 () throws Exception { 
        System.out.println ( "begin M1 --- Step" ); 
        B = 1000 ; 
        the Thread.sleep ( 5000 ); 
        System.out.println ( "M1 --- begin the second step" ); 
        System.out.println ( "B =" B +);  
     }
 
    public  the synchronized  void M2 () throws Exception {    
        System.out.println ( " m2 --- started the first step. " );
        Thread.sleep(2500);
        b = 2000;
        System.out.println("开始执行m2---第二步");
    }

    public void run() {
        try {
            m1();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {
        TT2 tt = new TT2();
        Thread t = new Thread(tt); 
        t.start();

        tt.m2();

        System.out.println(tt.b);  
    }
}

So the result is

Begin m2 --- first step 
begin M2 --- Step
 2000 
started M1 --- first step 
begin M1 --- Step 
b = 1000

 

Guess you like

Origin www.cnblogs.com/my_captain/p/12347383.html