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

Volatile or Synchroned not working for Java 14 or Java 15.0.1

    XMLWordPrintable

Details

    • x86_64
    • linux_ubuntu

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu 18.04, Java versions 14 and 15.0.1

      A DESCRIPTION OF THE PROBLEM :
      A program with multiple threads modifying the same object is not successfully synchronizing access to that object. The program works correctly under Java 8 and Java 11, but not under Java 14 or Java 15.0.1

      REGRESSION : Last worked in version 11

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The attached program creates 10 threads, each of which increments an Integer counter 100,000 times. The counter is declared as volatile and the increment operation is synchronized.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Final result 1000000
      ACTUAL -
      Result less than 1000000

      ---------- BEGIN SOURCE ----------
      public class CounterThread2 extends Thread {

      public static void main(String[] args) {
      Thread[] threads = new Thread[10];
      for (int i = 0; i < 10; ++i) {
      threads[i] = new CounterThread2("Counter-" + i);
      }
      for (Thread t : threads) {
      t.start();
      }

      // Wait for all threads to end
      boolean done = false;
      while (!done) {
      boolean alive = false;
      for (int i = 0; i < 10; ++i) {
      if (threads[i].isAlive()) alive = true;
      }
      done = !alive;
      }
      System.out.println("End - end - end");
      }

      // Counter variable
      private static volatile Integer counter = 0;

      // Object attributes
      private String threadName;

      public CounterThread2(String name) {
      super(name);
      this.threadName = name;
      }

      public void run() {
      for (int i = 0; i < 100000; i++) {
      synchronized(counter) {
      counter++;
      }
      System.out.println("Count " + counter + " (" + threadName + ")");
      }
      }
      }
      ---------- END SOURCE ----------

      FREQUENCY : often


      Attachments

        Activity

          People

            sswsharm swati sharma (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: