long and double assignments are not atomic - How does it matter?

前端 未结 3 1691
-上瘾入骨i
-上瘾入骨i 2021-02-10 17:07

We know that long and double assignments are not atomic in Java until they are declared volatile. My question is how does it really matter in our programming practice. for inst

3条回答
  •  我在风中等你
    2021-02-10 17:27

    I made a cool little example of this a while ago

    public class UnatomicLong implements Runnable {
        private static long test = 0;
    
        private final long val;
    
        public UnatomicLong(long val) {
            this.val = val;
        }
    
        @Override
        public void run() {
            while (!Thread.interrupted()) {
                test = val;
            }
        }
    
        public static void main(String[] args) {
            Thread t1 = new Thread(new UnatomicLong(-1));
            Thread t2 = new Thread(new UnatomicLong(0));
    
            System.out.println(Long.toBinaryString(-1));
            System.out.println(pad(Long.toBinaryString(0), 64));
    
            t1.start();
            t2.start();
    
            long val;
            while ((val = test) == -1 || val == 0) {
            }
    
            System.out.println(pad(Long.toBinaryString(val), 64));
            System.out.println(val);
    
            t1.interrupt();
            t2.interrupt();
        }
    
        // prepend 0s to the string to make it the target length
        private static String pad(String s, int targetLength) {
            int n = targetLength - s.length();
            for (int x = 0; x < n; x++) {
                s = "0" + s;
            }
            return s;
        }
    }
    

    One thread constantly tries to assign 0 to test while the other tries to assign -1. Eventually you'll end up with a number that's either 0b1111111111111111111111111111111100000000000000000000000000000000
    or
    0b0000000000000000000000000000000011111111111111111111111111111111.
    (Assuming you aren't on a 64 bit JVM. Most, if not all, 64 bit JVMs will actually do atomic assignment for longs and doubles.)

提交回复
热议问题