-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSynchronized.java
More file actions
47 lines (36 loc) · 888 Bytes
/
Synchronized.java
File metadata and controls
47 lines (36 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package concurrent.sync;
/**
* Created by Edwin Xu on 6/27/2020 1:42 PM
*
* 悲观锁Synchronized 怎么使用
*
*/
public class Synchronized {
private static int count =0;
public static synchronized void test(){
count++;
System.out.println(count);
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()->{test();},"线程"+i).start();
}
System.out.println("--------------------------------------");
for (int i = 0; i < 100; i++) {
new SynchronizeTest().run();
}
Integer a = 0;
synchronized(a) {
}
}
}
class SynchronizeTest implements Runnable{
private static int cnt=0;
@Override
public void run(){
synchronized (this){
cnt++;
System.out.println(cnt);
}
}
}