forked from CentMeng/JavaFrameTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiSyncCommonOneTest.java
More file actions
53 lines (46 loc) · 1.52 KB
/
Copy pathMultiSyncCommonOneTest.java
File metadata and controls
53 lines (46 loc) · 1.52 KB
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
48
49
50
51
52
53
package com.msj.sync;
/**
* @author Vincent.M mengshaojie@188.com
* @date 2017/3/13 下午4:05
* @copyright ©2017 孟少杰 All Rights Reserved
* @desc 屏蔽掉对象可以测试锁对象使用,让不同对象能够同步方法
*/
public class MultiSyncCommonOneTest {
private static volatile int count = 0;
public void addCount(){
// public void addCount(String lock){
// synchronized (lock) {
//或者 synchronized (this) {
//或者 synchronized(MultiSyncCommonOneTest.class){
count++;
// }
System.out.println(""+count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
for(int i=0;i<1000;i++){
//锁对象使用,让不同对象能够同步方法
// final String lock = "lock";
final MultiSyncCommonOneTest test1 = new MultiSyncCommonOneTest();
final MultiSyncCommonOneTest test2 = new MultiSyncCommonOneTest();
new Thread(new Runnable() {
@Override
public void run() {
test1.addCount();
// test1.addCount(lock);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
test2.addCount();
// test2.addCount(lock);
}
}).start();
}
}
}