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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab