1 4 package com.tc.util.concurrent; 5 6 import com.tc.exception.TCInternalError; 7 8 import java.io.Serializable ; 9 10 23 public final class SetOnceFlag implements Serializable { 24 27 private final Object lock = new LockObject(); 29 private volatile boolean set; 30 31 public SetOnceFlag() { 32 this(false); 33 } 34 35 40 public SetOnceFlag(final boolean setFlag) { 41 if (setFlag) { 42 this.set = true; 43 } 44 } 45 46 52 public void set() { 53 synchronized (lock) { 54 if (set) { 55 throw new IllegalStateException ("Flag has already been set"); 56 } else { 57 set = true; 58 } 59 } 60 } 61 62 72 public boolean attemptSet() { 73 try { 74 synchronized (lock) { 75 if (set) { return false; } 76 77 set(); 78 79 return set; 80 } 81 } catch (Throwable t) { 82 throw new TCInternalError(t); 84 } 85 } 86 87 112 public boolean isSet() { 113 return set; 114 } 115 116 public String toString() { 117 return "set: " + set; 118 } 119 120 private static class LockObject extends java.lang.Object implements Serializable { 122 } 124 125 } | Popular Tags |