Skip to content

Commit b26f4e6

Browse files
author
孟祥程
committed
锁对象使用,让不同对象共用锁
1 parent 37cfb87 commit b26f4e6

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/com/msj/sync/MultiSyncCommonOneTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
* @author Vincent.M mengshaojie@188.com
55
* @date 2017/3/13 下午4:05
66
* @copyright ©2017 孟少杰 All Rights Reserved
7-
* @desc
7+
* @desc 屏蔽掉对象可以测试锁对象使用,让不同对象能够同步方法
88
*/
99
public class MultiSyncCommonOneTest {
1010

1111
private static volatile int count = 0;
1212

1313
public void addCount(){
14-
count ++;
14+
// public void addCount(String lock){
15+
// synchronized (lock) {
16+
count++;
17+
// }
1518
System.out.println(""+count);
1619
try {
1720
Thread.sleep(1000);
@@ -22,23 +25,25 @@ public void addCount(){
2225

2326
public static void main(String[] args){
2427

25-
26-
2728
for(int i=0;i<1000;i++){
29+
//锁对象使用,让不同对象能够同步方法
30+
// final String lock = "lock";
2831
final MultiSyncCommonOneTest test1 = new MultiSyncCommonOneTest();
2932
final MultiSyncCommonOneTest test2 = new MultiSyncCommonOneTest();
3033

3134
new Thread(new Runnable() {
3235
@Override
3336
public void run() {
3437
test1.addCount();
38+
// test1.addCount(lock);
3539
}
3640
}).start();
3741

3842
new Thread(new Runnable() {
3943
@Override
4044
public void run() {
4145
test2.addCount();
46+
// test2.addCount(lock);
4247
}
4348
}).start();
4449
}

0 commit comments

Comments
 (0)