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 蓝牙耳机怎么查看日志
  • ¥15 R语言 拟时序分析降维图如何减少分支
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统