@Test public void testTryLock() throws Exception { final OptimisticReentrantLock sl = new OptimisticReentrantLock(); final Semaphore semaphore = new Semaphore(0); sl.tryLock(); assertEquals(Thread.currentThread(), sl.getOwner()); assertEquals(1, sl.getStamp()); sl.tryLock(); assertEquals(Thread.currentThread(), sl.getOwner()); assertEquals(2, sl.getStamp()); sl.tryLock(); assertEquals(Thread.currentThread(), sl.getOwner()); assertEquals(3, sl.getStamp()); Thread t1 = new Thread(new Runnable() { @Override public void run() { while (!sl.tryLock()) { } assertEquals(Thread.currentThread(), sl.getOwner()); assertEquals(1, sl.getStamp()); sl.unlock(); semaphore.release(1); } }); t1.start(); Thread.sleep(50); sl.unlock(); assertEquals(Thread.currentThread(), sl.getOwner()); assertEquals(2, sl.getStamp()); sl.unlock(); assertEquals(Thread.currentThread(), sl.getOwner()); assertEquals(1, sl.getStamp()); sl.unlock(); semaphore.acquire(1); assertEquals(null, sl.getOwner()); assertEquals(0, sl.getStamp()); }
@Override public boolean tryLock() { Thread currentThread = Thread.currentThread(); if (currentThread == owner.get()) { stamp.incrementAndGet(); return true; } boolean locked = owner.compareAndSet(null, currentThread); if (locked) { stamp.set(1); } return locked; }
OptimisticReentrantLock implements Lock { @Override public boolean tryLock() { Thread currentThread = Thread.currentThread(); if (currentThread == owner.get()) { stamp.incrementAndGet(); return true; } boolean locked = owner.compareAndSet(null, currentThread); if (locked) { stamp.set(1); } return locked; } }
OptimisticReentrantLock implements Lock { @Override public boolean tryLock() { Thread currentThread = Thread.currentThread(); if (currentThread == owner.get()) { stamp.incrementAndGet(); return true; } boolean locked = owner.compareAndSet(null, currentThread); if (locked) { stamp.set(1); } return locked; } OptimisticReentrantLock(); }
OptimisticReentrantLock implements Lock { @Override public boolean tryLock() { Thread currentThread = Thread.currentThread(); if (currentThread == owner.get()) { stamp.incrementAndGet(); return true; } boolean locked = owner.compareAndSet(null, currentThread); if (locked) { stamp.set(1); } return locked; } 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 boolean tryLock() { Thread currentThread = Thread.currentThread(); if (currentThread == owner.get()) { stamp.incrementAndGet(); return true; } boolean locked = owner.compareAndSet(null, currentThread); if (locked) { stamp.set(1); } return locked; } 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();  }