1 16 17 package org.apache.catalina.cluster.util; 18 19 50 51 public class SingleRemoveSynchronizedAddLock { 52 53 public SingleRemoveSynchronizedAddLock() { 54 } 55 56 public SingleRemoveSynchronizedAddLock(boolean dataAvailable) { 57 this.dataAvailable=dataAvailable; 58 } 59 60 67 private long addWaitTimeout = 10000L; 68 69 76 private long removeWaitTimeout = 30000L; 77 78 85 private Thread remover = null; 86 87 90 private boolean addLocked = false; 91 92 95 private boolean removeLocked = false; 96 97 101 private boolean removeEnabled = true; 102 103 108 private boolean dataAvailable = false; 109 110 113 public synchronized long getAddWaitTimeout() { 114 return addWaitTimeout; 115 } 116 117 120 public synchronized void setAddWaitTimeout(long timeout) { 121 addWaitTimeout = timeout; 122 } 123 124 127 public synchronized long getRemoveWaitTimeout() { 128 return removeWaitTimeout; 129 } 130 131 134 public synchronized void setRemoveWaitTimeout(long timeout) { 135 removeWaitTimeout = timeout; 136 } 137 138 143 public synchronized boolean isDataAvailable() { 144 return dataAvailable; 145 } 146 147 151 public synchronized boolean isAddLocked() { 152 return addLocked; 153 } 154 155 159 public synchronized boolean isRemoveLocked() { 160 return removeLocked; 161 } 162 163 167 public synchronized boolean isRemovePolling() { 168 if ( remover != null ) { 169 return true; 170 } 171 return false; 172 } 173 174 179 public synchronized void lockAdd() { 180 if ( addLocked || removeLocked ) { 181 do { 182 try { 183 wait(addWaitTimeout); 184 } catch ( InterruptedException e ) { 185 } 186 } while ( addLocked || removeLocked ); 187 } 188 addLocked=true; 189 } 190 191 197 public synchronized boolean lockRemove() { 198 removeLocked=false; 199 if ( ( addLocked || ! dataAvailable ) && removeEnabled ) { 200 remover=Thread.currentThread(); 201 do { 202 try { 203 wait(removeWaitTimeout); 204 } catch ( InterruptedException e ) { 205 } 206 } while ( ( addLocked || ! dataAvailable ) && removeEnabled ); 207 remover=null; 208 } 209 if ( removeEnabled ) { 210 removeLocked=true; 211 } 212 return removeLocked; 213 } 214 215 219 public synchronized void unlockAdd(boolean dataAvailable) { 220 addLocked=false; 221 this.dataAvailable=dataAvailable; 222 if ( ( remover != null ) && ( dataAvailable || ! removeEnabled ) ) { 223 remover.interrupt(); 224 } else { 225 notifyAll(); 226 } 227 } 228 229 234 public synchronized void unlockRemove() { 235 removeLocked=false; 236 dataAvailable=false; 237 notifyAll(); 238 } 239 240 243 public synchronized void abortRemove() { 244 removeEnabled=false; 245 if ( remover != null ) { 246 remover.interrupt(); 247 } 248 } 249 250 } 251 | Popular Tags |