leoxu1718 2017-02-14 09:44 采纳率: 100%
浏览 890
已采纳

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条回答 默认 最新

  • -逍遥剑- 2017-02-14 10:30
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 有卷积神经网络识别害虫的项目吗
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理