8201327: Make Sensor deeply immutably thread safe
authormartin
Tue, 10 Apr 2018 10:17:35 -0700
changeset 49558 b2bd13eafc1c
parent 49557 7b00ac6c11ab
child 49559 08d1b228e805
8201327: Make Sensor deeply immutably thread safe Reviewed-by: alanb, chegar, asmundak
src/java.management/share/classes/sun/management/MemoryPoolImpl.java
src/java.management/share/classes/sun/management/Sensor.java
--- a/src/java.management/share/classes/sun/management/MemoryPoolImpl.java	Tue Apr 10 10:49:17 2018 -0400
+++ b/src/java.management/share/classes/sun/management/MemoryPoolImpl.java	Tue Apr 10 10:17:35 2018 -0700
@@ -55,10 +55,10 @@
     private long  usageThreshold;
     private long  collectionThreshold;
 
-    private boolean usageSensorRegistered;
-    private boolean gcSensorRegistered;
-    private Sensor  usageSensor;
-    private Sensor  gcSensor;
+    private boolean usageSensorRegistered; // VM-initialized to false
+    private boolean gcSensorRegistered;    // VM-initialized to false
+    private final Sensor usageSensor;
+    private final Sensor gcSensor;
 
     MemoryPoolImpl(String name, boolean isHeap, long usageThreshold,
                    long gcThreshold) {
@@ -72,8 +72,6 @@
         this.collectionThresholdSupported = (gcThreshold >= 0);
         this.usageSensor = new PoolSensor(this, name + " usage sensor");
         this.gcSensor = new CollectionSensor(this, name + " collection sensor");
-        this.usageSensorRegistered = false;
-        this.gcSensorRegistered = false;
     }
 
     public String getName() {
@@ -290,7 +288,7 @@
      * unless the memory usage has returned below the threshold.
      */
     class PoolSensor extends Sensor {
-        MemoryPoolImpl pool;
+        final MemoryPoolImpl pool;
 
         PoolSensor(MemoryPoolImpl pool, String name) {
             super(name);
@@ -316,10 +314,10 @@
      * when the memory usage of a memory pool after GC is crossing
      * the collection threshold.
      * The VM will trigger this sensor in subsequent crossing
-     * regardless if the memory usage has changed siince the previous GC.
+     * regardless if the memory usage has changed since the previous GC.
      */
     class CollectionSensor extends Sensor {
-        MemoryPoolImpl pool;
+        final MemoryPoolImpl pool;
         CollectionSensor(MemoryPoolImpl pool, String name) {
             super(name);
             this.pool = pool;
--- a/src/java.management/share/classes/sun/management/Sensor.java	Tue Apr 10 10:49:17 2018 -0400
+++ b/src/java.management/share/classes/sun/management/Sensor.java	Tue Apr 10 10:17:35 2018 -0700
@@ -48,10 +48,10 @@
  */
 
 public abstract class Sensor {
-    private Object  lock;
-    private String  name;
-    private long    count;
-    private boolean on;
+    private final Object lock = new Object();
+    private final String name;
+    private long count;                 // VM-initialized to 0
+    private boolean on;                 // VM-initialized to false
 
     /**
      * Constructs a {@code Sensor} object.
@@ -60,9 +60,6 @@
      */
     public Sensor(String name) {
         this.name = name;
-        this.count = 0;
-        this.on = false;
-        this.lock = new Object();
     }
 
     /**