1 23 24 29 42 43 48 49 50 package com.sun.enterprise.util.sync; 51 52 import java.lang.Thread ; 53 import java.lang.InterruptedException ; 54 import java.lang.IllegalMonitorStateException ; 55 56 import com.sun.enterprise.util.pool.TimedoutException; 57 58 105 106 public class Lock { 107 108 private Thread owner = null; private int lockCount = 0; 111 121 public synchronized void acquireLock() throws InterruptedException { 122 while(! tryGetLock()) 123 wait(); 124 } 125 126 140 public synchronized void acquireLock( long waitTime) throws 141 InterruptedException , TimedoutException 142 { 143 if (tryGetLock() == false && waitTime != 0) { 144 if( waitTime == -1) 145 while(! tryGetLock()) 146 wait(); 147 else { 148 boolean isWaiting = true; 149 long startTime = System.currentTimeMillis(); 150 long timeRemaining = waitTime; 151 while( isWaiting) { 152 wait( timeRemaining); 153 if( tryGetLock()) 154 return; 155 timeRemaining = startTime + waitTime - System.currentTimeMillis(); 156 isWaiting = (timeRemaining > 0); 157 } 158 throw new TimedoutException(); 159 } 160 } 161 } 162 163 166 private boolean tryGetLock() { 168 Thread t = Thread.currentThread(); 169 if( owner == null) { 170 owner = t; 171 lockCount = 1; 172 return true; 173 } 174 if( owner == t) { 175 ++lockCount; 176 return true; 177 } 178 return false; 179 } 180 181 190 public synchronized void releaseLock() { 191 if(!owner.equals( Thread.currentThread())) 192 throw new IllegalMonitorStateException (); 193 if(--lockCount == 0) { 194 owner = null; 195 notify(); 196 } 197 } 198 199 204 public synchronized boolean isLocked() { return (owner != null); } 205 206 211 public Thread getOwner() { return this.owner; } 212 } 213 214 | Popular Tags |