+ public int reference() {
+ AtomicInteger i = refCount;
+ if (i == null)
+ return 0;
+ int result = i.incrementAndGet();
+ //System.out.println(this + ": reference: " + (result-1) + " -> " + result + " (" + System.identityHashCode(refCount) + ")");
+ return result;
+ }
+
+ public int dereference() {
+ AtomicInteger i = refCount;
+ if (i == null)
+ return 0;
+ int result = i.decrementAndGet();
+ //System.out.println(this + ": dereference: " + (result+1) + " -> " + result + " (" + System.identityHashCode(refCount) + ")");
+ if (result <= 0) {
+ synchronized (i) {
+ i.notifyAll();
+ }
+ }
+ return result;
+ }
+
+ public void waitUntilNotReferenced() throws InterruptedException {
+ AtomicInteger i = refCount;
+ if (i == null)
+ return;
+ synchronized (i) {
+ while (i.get() > 0) {
+ i.wait();
+ }
+ }
+ }
+