@Test public void testLock() { OptimisticReentrantLock sl = new OptimisticReentrantLock(); sl.lock(); assertEquals(Thread.currentThread(), sl.getOwner()); assertEquals(1, sl.getStamp()); sl.unlock(); assertEquals(null, sl.getOwner()); assertEquals(0, sl.getStamp()); }
@Override public void lock() { Thread currentThread = Thread.currentThread(); if (currentThread == owner.get()) { stamp.incrementAndGet(); } else { while (!owner.compareAndSet(null, currentThread)) { } stamp.set(1); } }
OptimisticReentrantLock implements Lock { @Override public void lock() { Thread currentThread = Thread.currentThread(); if (currentThread == owner.get()) { stamp.incrementAndGet(); } else { while (!owner.compareAndSet(null, currentThread)) { } stamp.set(1); } } }
OptimisticReentrantLock implements Lock { @Override public void lock() { Thread currentThread = Thread.currentThread(); if (currentThread == owner.get()) { stamp.incrementAndGet(); } else { while (!owner.compareAndSet(null, currentThread)) { } stamp.set(1); } } OptimisticReentrantLock(); }
OptimisticReentrantLock implements Lock { @Override public void lock() { Thread currentThread = Thread.currentThread(); if (currentThread == owner.get()) { stamp.incrementAndGet(); } else { while (!owner.compareAndSet(null, currentThread)) { } stamp.set(1); } } OptimisticReentrantLock(); @Override void lock(); @Override void unlock(); @Override void lockInterruptibly(); @Override boolean tryLock(); @Override boolean tryLock(long time, TimeUnit unit); @Override Condition newCondition(); Thread getOwner(); int getStamp(); }
OptimisticReentrantLock implements Lock { @Override public void lock() { Thread currentThread = Thread.currentThread(); if (currentThread == owner.get()) { stamp.incrementAndGet(); } else { while (!owner.compareAndSet(null, currentThread)) { } stamp.set(1); } } OptimisticReentrantLock(); @Override void lock(); @Override void unlock(); @Override void lockInterruptibly(); @Override boolean tryLock(); @Override boolean tryLock(long time, TimeUnit unit); @Override Condition newCondition(); Thread getOwner(); int getStamp();  }