- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
import java.io.PrintStream;
import java.util.concurrent.atomic.AtomicBoolean;
public final class ShredingerCat
extends AtomicBoolean
{
static final PrintStream o=System.out;
static final long initMsec=System.currentTimeMillis ();
public static final ShredingerCat INSTANCE = new ShredingerCat();
private ShredingerCat ()
{
set ( false );
o.println ( "Construct SingleBool" );
}
public final
void criticalSection(){
synchronized (this) {
pr ( "Enter critical section" );
ShredingerCat.sl ( 5 );
pr ( "Cat is " +(
get()
? "dead"
: "alive"
)
);
sl(100);
pr ( "Exit critical section" );
}
}
// ===================== HELPER STUFF ========================
static void pr(String s){
o.println ( s+(
System.currentTimeMillis ()- initMsec
));
}
static void sl(long l){
try {
Thread.sleep ( l );
}catch (InterruptedException e) {
}
}
}