???????Synchronized????????
????Synchronized??Java?н?????????????????????????????????????Synchronized???????????????????1????????????????????????2????????????????????????????3????Ч??????????????????????Synchronized??????????÷???
??????1?????????????
??????2?????ξ??????
??????3?????δ????
???????????????????????????????????????????÷????????????????δ??????Synchronized????÷???????????????????????£???
????1???????????????
????????????
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             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 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
29         new Thread(new Runnable() {
30             @Override
31             public void run() {
32                 test.method1();
33             }
34         }).start();
35
36         new Thread(new Runnable() {
37             @Override
38             public void run() {
39                 test.method2();
40             }
41         }).start();
42     }
43 }
??????н?????£????1?????2????????????????2??????????1?????????2?????????????????????1?????2??????е??
????Method 1 start
????Method 1 execute
????Method 2 start
????Method 2 execute
????Method 2 end
????Method 1 end
????2????????????????
????????ζ???
1 package com.paddx.test.concurrent;
2
3 public class SynchronizedTest {
4     public 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 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
29         new Thread(new Runnable() {
30             @Override
31             public void run() {
32                 test.method1();
33             }
34         }).start();
35
36         new Thread(new Runnable() {
37             @Override
38             public void run() {
39                 test.method2();
40             }
41         }).start();
42     }
43 }
??????н?????£???????????????????????????????2?????????1??method1??????????????method2??????
????Method 1 start
????Method 1 execute
????Method 1 end
????Method 2 start
????Method 2 execute
????Method 2 end