1 7 8 package java.util.concurrent.locks; 9 import java.util.*; 10 import java.util.concurrent.*; 11 import java.util.concurrent.atomic.*; 12 13 81 public class ReentrantLock implements Lock , java.io.Serializable { 82 private static final long serialVersionUID = 7373984872572414699L; 83 84 private final Sync sync; 85 86 91 static abstract class Sync extends AbstractQueuedSynchronizer { 92 93 transient Thread owner; 94 95 99 abstract void lock(); 100 101 106 final boolean nonfairTryAcquire(int acquires) { 107 final Thread current = Thread.currentThread(); 108 int c = getState(); 109 if (c == 0) { 110 if (compareAndSetState(0, acquires)) { 111 owner = current; 112 return true; 113 } 114 } 115 else if (current == owner) { 116 setState(c+acquires); 117 return true; 118 } 119 return false; 120 } 121 122 protected final boolean tryRelease(int releases) { 123 int c = getState() - releases; 124 if (Thread.currentThread() != owner) 125 throw new IllegalMonitorStateException (); 126 boolean free = false; 127 if (c == 0) { 128 free = true; 129 owner = null; 130 } 131 setState(c); 132 return free; 133 } 134 135 protected final boolean isHeldExclusively() { 136 return getState() != 0 && owner == Thread.currentThread(); 137 } 138 139 final ConditionObject newCondition() { 140 return new ConditionObject(); 141 } 142 143 145 final Thread getOwner() { 146 int c = getState(); 147 Thread o = owner; 148 return (c == 0)? null : o; 149 } 150 151 final int getHoldCount() { 152 int c = getState(); 153 Thread o = owner; 154 return (o == Thread.currentThread())? c : 0; 155 } 156 157 final boolean isLocked() { 158 return getState() != 0; 159 } 160 161 165 private void readObject(java.io.ObjectInputStream s) 166 throws java.io.IOException , ClassNotFoundException { 167 s.defaultReadObject(); 168 setState(0); } 170 } 171 172 175 final static class NonfairSync extends Sync { 176 180 final void lock() { 181 if (compareAndSetState(0, 1)) 182 owner = Thread.currentThread(); 183 else 184 acquire(1); 185 } 186 187 protected final boolean tryAcquire(int acquires) { 188 return nonfairTryAcquire(acquires); 189 } 190 } 191 192 195 final static class FairSync extends Sync { 196 final void lock() { 197 acquire(1); 198 } 199 200 204 protected final boolean tryAcquire(int acquires) { 205 final Thread current = Thread.currentThread(); 206 int c = getState(); 207 if (c == 0) { 208 Thread first = getFirstQueuedThread(); 209 if ((first == null || first == current) && 210 compareAndSetState(0, acquires)) { 211 owner = current; 212 return true; 213 } 214 } 215 else if (current == owner) { 216 setState(c+acquires); 217 return true; 218 } 219 return false; 220 } 221 } 222 223 227 public ReentrantLock() { 228 sync = new NonfairSync(); 229 } 230 231 236 public ReentrantLock(boolean fair) { 237 sync = (fair)? new FairSync() : new NonfairSync(); 238 } 239 240 255 public void lock() { 256 sync.lock(); 257 } 258 259 306 public void lockInterruptibly() throws InterruptedException { 307 sync.acquireInterruptibly(1); 308 } 309 310 337 public boolean tryLock() { 338 return sync.nonfairTryAcquire(1); 339 } 340 341 415 public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { 416 return sync.tryAcquireNanos(1, unit.toNanos(timeout)); 417 } 418 419 430 public void unlock() { 431 sync.release(1); 432 } 433 434 473 public Condition newCondition() { 474 return sync.newCondition(); 475 } 476 477 507 public int getHoldCount() { 508 return sync.getHoldCount(); 509 } 510 511 553 public boolean isHeldByCurrentThread() { 554 return sync.isHeldExclusively(); 555 } 556 557 564 public boolean isLocked() { 565 return sync.isLocked(); 566 } 567 568 572 public final boolean isFair() { 573 return sync instanceof FairSync; 574 } 575 576 585 protected Thread getOwner() { 586 return sync.getOwner(); 587 } 588 589 599 public final boolean hasQueuedThreads() { 600 return sync.hasQueuedThreads(); 601 } 602 603 604 615 public final boolean hasQueuedThread(Thread thread) { 616 return sync.isQueued(thread); 617 } 618 619 620 629 public final int getQueueLength() { 630 return sync.getQueueLength(); 631 } 632 633 643 protected Collection<Thread > getQueuedThreads() { 644 return sync.getQueuedThreads(); 645 } 646 647 662 public boolean hasWaiters(Condition condition) { 663 if (condition == null) 664 throw new NullPointerException (); 665 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject )) 666 throw new IllegalArgumentException ("not owner"); 667 return sync.hasWaiters((AbstractQueuedSynchronizer.ConditionObject )condition); 668 } 669 670 685 public int getWaitQueueLength(Condition condition) { 686 if (condition == null) 687 throw new NullPointerException (); 688 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject )) 689 throw new IllegalArgumentException ("not owner"); 690 return sync.getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject )condition); 691 } 692 693 710 protected Collection<Thread > getWaitingThreads(Condition condition) { 711 if (condition == null) 712 throw new NullPointerException (); 713 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject )) 714 throw new IllegalArgumentException ("not owner"); 715 return sync.getWaitingThreads((AbstractQueuedSynchronizer.ConditionObject )condition); 716 } 717 718 725 public String toString() { 726 Thread owner = sync.getOwner(); 727 return super.toString() + ((owner == null) ? 728 "[Unlocked]" : 729 "[Locked by thread " + owner.getName() + "]"); 730 } 731 } 732 | Popular Tags |