Toad schrieb: > > Is ++'ing an integer always atomic in java, assuming the var is declared > volatile? AFAICS, x86 would be atomic... but anything RISC would involve > a separate load and store, right?
it is not - even on x86. Looked though my java files and found the one i used to test this. mihi -------------- next part -------------- class ThreadDemo implements Runnable { private Thread t; private volatile int local; private static volatile int global; public static final int COUNT = 2; public static int HOWFAR = 10000000; public static int TESTNUM = 10; public static Object o = new Object(); public ThreadDemo () { (t=new Thread(this)).start(); } public void run () { int max = (int) (HOWFAR * Math.random()); for (int i=0;i < max; i++) { local++; global++; } } public static void main(String[] args) { for(int i = 0; i<TESTNUM; i++) dotest(); } public static void dotest () { ThreadDemo[] tda = new ThreadDemo[COUNT]; global=0; for (int i=0; i<tda.length; i++) { tda[i] = new ThreadDemo(); } long sum = 0; for (int i=0; i<tda.length; i++) { try { tda[i].t.join(); } catch (InterruptedException e) {System.exit(0);} sum += tda[i].local; } System.out.println ("Lokal: "+sum+", Global: "+global+ ", also "+ (sum==global ? "okay" : "Fehler: "+ (sum-global))); } }