File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 */
99public 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 }
You can’t perform that action at this time.
0 commit comments