8201327: Make Sensor deeply immutably thread safe

Reviewed-by: alanb, chegar, asmundak
This commit is contained in:
Martin Buchholz 2018-04-10 10:17:35 -07:00
parent 0eff8e94f0
commit 7c5c5acb6e
2 changed files with 11 additions and 16 deletions

View file

@ -55,10 +55,10 @@ class MemoryPoolImpl implements MemoryPoolMXBean {
private long usageThreshold; private long usageThreshold;
private long collectionThreshold; private long collectionThreshold;
private boolean usageSensorRegistered; private boolean usageSensorRegistered; // VM-initialized to false
private boolean gcSensorRegistered; private boolean gcSensorRegistered; // VM-initialized to false
private Sensor usageSensor; private final Sensor usageSensor;
private Sensor gcSensor; private final Sensor gcSensor;
MemoryPoolImpl(String name, boolean isHeap, long usageThreshold, MemoryPoolImpl(String name, boolean isHeap, long usageThreshold,
long gcThreshold) { long gcThreshold) {
@ -72,8 +72,6 @@ class MemoryPoolImpl implements MemoryPoolMXBean {
this.collectionThresholdSupported = (gcThreshold >= 0); this.collectionThresholdSupported = (gcThreshold >= 0);
this.usageSensor = new PoolSensor(this, name + " usage sensor"); this.usageSensor = new PoolSensor(this, name + " usage sensor");
this.gcSensor = new CollectionSensor(this, name + " collection sensor"); this.gcSensor = new CollectionSensor(this, name + " collection sensor");
this.usageSensorRegistered = false;
this.gcSensorRegistered = false;
} }
public String getName() { public String getName() {
@ -290,7 +288,7 @@ class MemoryPoolImpl implements MemoryPoolMXBean {
* unless the memory usage has returned below the threshold. * unless the memory usage has returned below the threshold.
*/ */
class PoolSensor extends Sensor { class PoolSensor extends Sensor {
MemoryPoolImpl pool; final MemoryPoolImpl pool;
PoolSensor(MemoryPoolImpl pool, String name) { PoolSensor(MemoryPoolImpl pool, String name) {
super(name); super(name);
@ -316,10 +314,10 @@ class MemoryPoolImpl implements MemoryPoolMXBean {
* when the memory usage of a memory pool after GC is crossing * when the memory usage of a memory pool after GC is crossing
* the collection threshold. * the collection threshold.
* The VM will trigger this sensor in subsequent crossing * 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 { class CollectionSensor extends Sensor {
MemoryPoolImpl pool; final MemoryPoolImpl pool;
CollectionSensor(MemoryPoolImpl pool, String name) { CollectionSensor(MemoryPoolImpl pool, String name) {
super(name); super(name);
this.pool = pool; this.pool = pool;

View file

@ -48,10 +48,10 @@ import java.util.HashMap;
*/ */
public abstract class Sensor { public abstract class Sensor {
private Object lock; private final Object lock = new Object();
private String name; private final String name;
private long count; private long count; // VM-initialized to 0
private boolean on; private boolean on; // VM-initialized to false
/** /**
* Constructs a {@code Sensor} object. * Constructs a {@code Sensor} object.
@ -60,9 +60,6 @@ public abstract class Sensor {
*/ */
public Sensor(String name) { public Sensor(String name) {
this.name = name; this.name = name;
this.count = 0;
this.on = false;
this.lock = new Object();
} }
/** /**