sclass TestPhantomCleanUp {
settable bool withGC = true;
settable double maxWait = 60.0; // how many seconds
gettable bool referenceWasCleared;
gettable Duration delayBeforeClearance;
run {
temp new RunnablesReferenceQueue queue;
class MyPhantomRef extends PhantomReference is Runnable {
new Flag flag;
*(A a, RunnablesReferenceQueue queue) {
super(a, queue!);
}
run {
print("Behold the phantom!");
flag.raise();
}
}
var ref = new MyPhantomRef(new S("copy of a string"), queue);
if (withGC) timedGC();
long time = sysNow();
printWithMS("Waiting...");
if (ref.flag.waitUntilUp(toMS_int(maxWait))) {
delayBeforeClearance = msToDuration(sysNow()-time);
referenceWasCleared = true;
printWithMS("Cool! Reference was cleared after "
+ renderElapsedSecondsPleasantly(delayBeforeClearance));
} else
printWithMS("Ouch");
}
}