Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8344530

Spinlock with increment ++i does not work

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Windows 11, x64

      A DESCRIPTION OF THE PROBLEM :
      When you write your custom spinlock, it doesn’t work with ++i, but it works with i+1


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      write code

      ACTUAL -
      0 and deadlock

      ---------- BEGIN SOURCE ----------

      import java.util.concurrent.atomic.AtomicInteger;

      public class Main {
          private static AtomicInteger atomicInteger = new AtomicInteger(0);

          public static void main(String[] args) throws InterruptedException {

              for (int i = 0; i < 100; ++i) {
                  var t1 = new Thread(() -> {
                      for(;;) {
                          int saved = atomicInteger.get();

                          int newValue = ++saved;

                          if(atomicInteger.compareAndSet(saved, newValue)) {
                              return;
                          }
                      }
                  });

                  var t2 = new Thread(() -> {
                      for(;;) {
                          int saved = atomicInteger.get();

                          int newValue = ++saved;

                          if(atomicInteger.compareAndSet(saved, newValue)) {
                              return;
                          }
                      }
                  });

                  t1.start();
                  t2.start();
              }

              Thread.sleep(1000);

              System.out.println(atomicInteger.get());
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :

      import java.util.concurrent.atomic.AtomicInteger;

      public class Main {
          private static AtomicInteger atomicInteger = new AtomicInteger(0);

          public static void main(String[] args) throws InterruptedException {

              for (int i = 0; i < 100; ++i) {
                  var t1 = new Thread(() -> {
                      for(;;) {
                          int saved = atomicInteger.get();

                          int newValue = saved + 1;

                          if(atomicInteger.compareAndSet(saved, newValue)) {
                              return;
                          }
                      }
                  });

                  var t2 = new Thread(() -> {
                      for(;;) {
                          int saved = atomicInteger.get();

                          int newValue = saved + 1;

                          if(atomicInteger.compareAndSet(saved, newValue)) {
                              return;
                          }
                      }
                  });

                  t1.start();
                  t2.start();
              }

              Thread.sleep(1000);

              System.out.println(atomicInteger.get());
          }
      }

      FREQUENCY : always


            tongwan Andrew Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: