Skip to content

Commit 50ecfa4

Browse files
committed
synchronized关键字理解
1 parent 17c1c85 commit 50ecfa4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.scma.synchronized_test;
2+
3+
/*
4+
* 关键字synchronized 原理
5+
* */
6+
public class synchronizedTest implements Runnable {
7+
public static int num = 0;
8+
9+
@Override
10+
public void run() {
11+
for (int i = 0; i < 1000; i++) {
12+
num++;
13+
}
14+
}
15+
16+
public static void main(String[] args) {
17+
for(int i=0;i<100;i++){
18+
new Thread(new synchronizedTest()).start();
19+
}
20+
try {
21+
Thread.sleep(2000); //等所有线程执行++ 操作完成
22+
} catch ( Exception e) {
23+
24+
}
25+
System.out.println(num);
26+
}
27+
}

0 commit comments

Comments
 (0)