Relation between bytecode instructions and processor operations

后端 未结 3 550
无人共我
无人共我 2021-02-06 01:45

Java specification guarantees primitive variable assignments are always atomic (expect for long and double types.

On the contrary, Fetch-and-Ad

3条回答
  •  时光说笑
    2021-02-06 02:20

    Regarding your first question: the read and the write are atomic, but the read/write operation is not. I could not find a specific reference on primitives but the JLS #17.7 says something similar regarding references:

    Writes to and reads of references are always atomic, regardless of whether they are implemented as 32-bit or 64-bit values.

    So in your case, both the iload and istore are atomic, but the whole (iload, istore) operation is not.

    Is it wrong to [consider that] there's no need at all to synchronize the i++ operation?

    Regarding your second question, the code below prints 982 on my x86 machine (and not 1,000) which shows that some ++ got lost in translation ==> you need to properly synchronize a ++ operation even on a processor architecture that supports a fetch-and-add instruction.

    public class Test1 {
    
        private static int i = 0;
    
        public static void main(String args[]) throws InterruptedException {
            ExecutorService executor = Executors.newFixedThreadPool(10);
            final CountDownLatch start = new CountDownLatch(1);
            final Set set = new ConcurrentSkipListSet<>();
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        start.await();
                    } catch (InterruptedException ignore) {}
                    for (int j = 0; j < 100; j++) {
                        set.add(i++);
                    }
                }
            };
    
            for (int j = 0; j < 10; j++) {
                executor.submit(r);
            }
            start.countDown();
            executor.shutdown();
            executor.awaitTermination(1, TimeUnit.SECONDS);
            System.out.println(set.size());
        }
    }
    

提交回复
热议问题