2015-04-01 58 views
0

我在調試以下代碼。檢查變量似乎正在改變運行的行爲

ArrayList<ArrayList<LinkedList<FileInfo>>> cache = emptyCache(); 

private void addToCache(final FileInfo fileInfo) { 
    final int first = 0xFF & Byte.valueOf(fileInfo.getFirstByteFromMiddle()); 
    final int second = 0xFF & Byte.valueOf(fileInfo.getSecondByteFromMiddle()); 

    if (first < 0 || second < 0) 
    System.err.println("error"); 

    cache.get(first).get(second).add(fileInfo); 
} 

不上System.err.println("error")設置斷點,這是我觀察到控制檯上。

/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:58469,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Users/petur/Development/Einfalda/einfalda/target/classes:/Users/petur/.m2/repository/org/apache/commons/commons-collections4/4.0/commons-collections4-4.0.jar:/Users/petur/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/Applications/IntelliJ IDEA 14.app/Contents/lib/idea_rt.jar" eu.petur.einfalda.Einfalda /Users/petur/Documents 
Connected to the target VM, address: '127.0.0.1:58469', transport: 'socket' 
Disconnected from the target VM, address: '127.0.0.1:58469', transport: 'socket' 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -125 
    at java.util.ArrayList.elementData(ArrayList.java:418) 
    at java.util.ArrayList.get(ArrayList.java:431) 
    at eu.petur.einfalda.DupFinder.addToCache(DupFinder.java:94) 
    at eu.petur.einfalda.DupFinder.findDuplicates(DupFinder.java:30) 
    at eu.petur.einfalda.Einfalda.main(Einfalda.java:23) 

Process finished with exit code 1 

但是,如果我設置斷點與System.err.println("error")行程序運行良好,並與Process finished with exit code 0退出。 同樣,如果我使用System.out.println顯示這兩個變量的值,就在它們被設置後。

爲什麼在使用變量前檢查變量時不能重現該錯誤?

+0

我使用'0xFF&'將字節的域從[-128; 127]轉換爲[0;∞)。 – 2015-04-01 09:59:38

+0

代碼是否在併發環境中使用? – ChristopherZ 2015-04-01 10:01:00

+0

也許它運行速度快嗎? – 2015-04-01 10:01:19

回答

0

我通過使用-Djava.compiler=NONE作爲VM選項禁用JIT來解決了這個問題。我只能想象這個問題與優化有關。