????3????????????????
?????????????
1 package com.paddx.test.concurrent;
2
3  public class SynchronizedTest {
4      public static synchronized void method1(){
5          System.out.println("Method 1 start");
6          try {
7              System.out.println("Method 1 execute");
8              Thread.sleep(3000);
9          } catch (InterruptedException e) {
10              e.printStackTrace();
11          }
12          System.out.println("Method 1 end");
13      }
14
15      public static synchronized void method2(){
16          System.out.println("Method 2 start");
17          try {
18              System.out.println("Method 2 execute");
19              Thread.sleep(1000);
20          } catch (InterruptedException e) {
21              e.printStackTrace();
22          }
23          System.out.println("Method 2 end");
24      }
25
26      public static void main(String[] args) {
27          final SynchronizedTest test = new SynchronizedTest();
28          final SynchronizedTest test2 = new SynchronizedTest();
29
30          new Thread(new Runnable() {
31              @Override
32              public void run() {
33                  test.method1();
34              }
35          }).start();
36
37          new Thread(new Runnable() {
38              @Override
39              public void run() {
40                  test2.method2();
41              }
42          }).start();
43      }
44  }
??????н?????£???????????????????????????????????????????????????????????????????????????????????test??test2????????????????????????SynchronizedTest???????????????????????method1??method2???????????С?
????Method 1 start
????Method 1 execute
????Method 1 end
????Method 2 start
????Method 2 execute
????Method 2 end
????4??????????
????????????
1 package com.paddx.test.concurrent;
2
3 public class SynchronizedTest {
4     public void method1(){
5         System.out.println("Method 1 start");
6         try {
7             synchronized (this) {
8                 System.out.println("Method 1 execute");
9                 Thread.sleep(3000);
10             }
11         } catch (InterruptedException e) {
12             e.printStackTrace();
13         }
14         System.out.println("Method 1 end");
15     }
16
17     public void method2(){
18         System.out.println("Method 2 start");
19         try {
20             synchronized (this) {
21                 System.out.println("Method 2 execute");
22                 Thread.sleep(1000);
23             }
24         } catch (InterruptedException e) {
25             e.printStackTrace();
26         }
27         System.out.println("Method 2 end");
28     }
29
30     public static void main(String[] args) {
31         final SynchronizedTest test = new SynchronizedTest();
32
33         new Thread(new Runnable() {
34             @Override
35             public void run() {
36                 test.method1();
37             }
38         }).start();
39
40         new Thread(new Runnable() {
41             @Override
42             public void run() {
43                 test.method2();
44             }
45         }).start();
46     }
47 }
??????н?????£???????1?????2????????????????????У????????2???????????????????????1????????????ɡ?
????Method 1 start
????Method 1 execute
????Method 2 start
????Method 1 end
????Method 2 execute
????Method 2 end