java - volatile中i++的原子性问题
怪我咯
怪我咯 2017-04-18 10:52:11
0
3
579
/**
 * @create on 17/3/27
 * @description
 */
public class Main {
    static volatile int i = 0;
    public static class PlusTask implements Runnable{
        @Override
        public void run(){
            for(int k=0; k<10000; k++)
                i++;
        }
    }

    public static void main(String[] args) throws InterruptedException{
        Thread[] threads = new Thread[10];
        for(int i=0;i<10;i++){
            threads[i] = new Thread(new PlusTask());
            threads[i].start();
        }

        for(int i=0;i<10;i++){
            threads[i].join();
        }

        System.out.println(i);
    }
}

请教各位大牛 为什么这里的输出总是小于10000? 已经调用了thread.join

怪我咯
怪我咯

走同样的路,发现不同的人生

人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!