Java, 关于并发问题,实例“xx”虽然在同步块中操作,但貌似还是会被多个线程操作到,求大神指导~

public class test333 {

 volatile Integer xx =0;


@Test
public void xxxx(){

    int threads = 100;

    final Vector<Integer> lst = new Vector<Integer>();
    ExecutorService executorService = Executors.newFixedThreadPool(threads);
    final CountDownLatch countDownLatch = new CountDownLatch(threads);
    for(int i =0;i<threads;i++){
        executorService.submit(new Runnable() {
            public void run() {
                synchronized (this) {
                    xx++;
                    if(xx%2==0){
                        System.out.println(" the num is 【"+xx+"】  the double ......");
                    }else{
                        System.out.println("the num is 【"+xx+"】 is the single......");
                    }   
                    lst.add(xx);
                    countDownLatch.countDown();
                }
                }                       
        });
    }

    try {

        countDownLatch.await();

        System.out.println("【Finish!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!】");

        for(int i=1;i<threads;i++){
            if(!lst.contains(i)){
                System.out.println("the current 【"+i+"】is not in the lst //t//n");
            }
        }

    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

}

---------------------------------执行结果-----------------------------------
the num is 【1】 is the single......
the num is 【2】 the double ......
the num is 【3】 is the single......
the num is 【4】 the double ......
the num is 【5】 is the single......
the num is 【6】 the double ......
the num is 【7】 is the single......
the num is 【8】 the double ......
the num is 【9】 is the single......
the num is 【10】 the double ......
the num is 【11】 is the single......
the num is 【12】 the double ......
the num is 【13】 is the single......
the num is 【14】 the double ......
the num is 【15】 is the single......
the num is 【16】 the double ......
the num is 【17】 is the single......
the num is 【18】 the double ......
the num is 【20】 the double ......
the num is 【19】 is the single......
the num is 【21】 is the single......
the num is 【22】 the double ......
the num is 【27】 is the single......
the num is 【26】 the double ......
the num is 【25】 is the single......
the num is 【29】 is the single......
the num is 【24】 the double ......
the num is 【23】 is the single......
the num is 【28】 the double ......
the num is 【30】 the double ......
the num is 【31】 is the single......
the num is 【32】 the double ......
the num is 【33】 is the single......
the num is 【34】 the double ......
the num is 【35】 is the single......
the num is 【36】 the double ......
the num is 【37】 is the single......
the num is 【38】 the double ......
the num is 【39】 is the single......
the num is 【40】 the double ......
the num is 【41】 is the single......
the num is 【42】 the double ......
the num is 【43】 is the single......
the num is 【44】 the double ......
the num is 【45】 is the single......
the num is 【46】 the double ......
the num is 【47】 is the single......
the num is 【48】 the double ......
the num is 【49】 is the single......
the num is 【50】 the double ......
the num is 【51】 is the single......
the num is 【52】 the double ......
the num is 【53】 is the single......
the num is 【54】 the double ......
the num is 【55】 is the single......
the num is 【56】 the double ......
the num is 【57】 is the single......
the num is 【58】 the double ......
the num is 【59】 is the single......
the num is 【60】 the double ......
the num is 【61】 is the single......
the num is 【62】 the double ......
the num is 【63】 is the single......
the num is 【64】 the double ......
the num is 【65】 is the single......
the num is 【66】 the double ......
the num is 【67】 is the single......
the num is 【69】 is the single......
the num is 【69】 the double ......
the num is 【70】 the double ......
the num is 【71】 is the single......
the num is 【72】 the double ......
the num is 【73】 is the single......
the num is 【74】 the double ......
the num is 【75】 is the single......
the num is 【76】 the double ......
the num is 【77】 is the single......
the num is 【78】 the double ......
the num is 【79】 is the single......
the num is 【80】 the double ......
the num is 【81】 is the single......
the num is 【82】 the double ......
the num is 【83】 is the single......
the num is 【84】 the double ......
the num is 【85】 is the single......
the num is 【86】 the double ......
the num is 【87】 is the single......
the num is 【88】 the double ......
the num is 【89】 is the single......
the num is 【90】 the double ......
the num is 【91】 is the single......
the num is 【92】 the double ......
the num is 【93】 is the single......
the num is 【94】 the double ......
the num is 【95】 is the single......
the num is 【96】 the double ......
the num is 【97】 is the single......
the num is 【98】 the double ......
the num is 【99】 is the single......
the num is 【100】 the double ......

【Finish!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!】

the current 【3】is not in the lst //t//n
the current 【8】is not in the lst //t//n
the current 【18】is not in the lst //t//n
the current 【22】is not in the lst //t//n
the current 【23】is not in the lst //t//n
the current 【24】is not in the lst //t//n
the current 【25】is not in the lst //t//n
the current 【68】is not in the lst //t//n
the current 【71】is not in the lst //t//n

2个回答

synchronized (this)这个有问题吧。。。this是匿名类,不应该指向匿名类

你这里this是各个runnable的对象,根本没有达到加锁的目的。应该用同一个对象来synchronized

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!