Java????????Synchronized??????????
???????????? ???????[ 2016/4/29 13:41:48 ] ??????????????????? Java
???????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
??????
![](/images/ad-banner/ad-banner.png)
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11