第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

一個簡單多線程資源共享問題,不明白為什么會出現(xiàn)奇數(shù)結果

一個簡單多線程資源共享問題,不明白為什么會出現(xiàn)奇數(shù)結果

富國滬深 2019-03-20 15:15:52
代碼為下面:為什么運行會出現(xiàn)奇數(shù)結果? 《java編程思想》的里面的解釋沒有看懂,能夠請大神解釋一下其中原理。import java.util.concurrent.*;public class EvenGenerator extends IntGenerator {  private int currentEvenValue = 0;  public int next() {    ++currentEvenValue; // Danger point here!    ++currentEvenValue;    return currentEvenValue;  }  public static void main(String[] args) {    EvenChecker.test(new EvenGenerator() ,10);  }} public class EvenChecker implements Runnable {  private IntGenerator generator;  private final int id;  public EvenChecker(IntGenerator g, int ident) {    generator = g;    id = ident;  }  public void run() {    while(!generator.isCanceled()) {      int val = generator.next();    //   System . out . println ( val ) ;               if(val % 2 != 0) {        System.out.println(val + " not even!");        generator.cancel(); // Cancels all EvenCheckers      }    }  }  // Test any type of IntGenerator:  public static void test(IntGenerator gp, int count) {    System.out.println("Press Control-C to exit");    ExecutorService exec = Executors.newCachedThreadPool();    for(int i = 0; i < count; i++)      exec.execute(new EvenChecker(gp, i));    exec.shutdown();  }   }}
查看完整描述

2 回答

?
拉莫斯之舞

TA貢獻1820條經(jīng)驗 獲得超10個贊

第一個問題, 因為i++不是原子操作,stackOverflow回答
第二個問題, 是因為你試的次數(shù)不多。

查看完整回答
反對 回復 2019-04-17
  • 2 回答
  • 0 關注
  • 635 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號