core/src/main/java/com/orientechnologies/common/concur/lock/ScalableRWLock.java
public boolean exclusiveTryLockNanos(long nanosTimeout) throws InterruptedException {
    final long lastTime = System.nanoTime();
    // Try to acquire the lock in write-mode
    if (stampedLock.tryWriteLock(nanosTimeout, TimeUnit.NANOSECONDS) == 0) {
      return false;
    }

    // We can only do this after writerOwner has been set to the current thread
    AtomicInteger[] localReadersStateArray = readersStateArrayRef.get();
    if (localReadersStateArray == null) {
      // Set to dummyArray before scanning the readersStateList to impose
      // a linearizability condition
      readersStateArrayRef.set(dummyArray);
      // Copy readersStateList to an array
      localReadersStateArray = readersStateList.toArray(new AtomicInteger[readersStateList.size()]);
      readersStateArrayRef.compareAndSet(dummyArray, localReadersStateArray);
    }

    // Scan the array of Reader states
    for (AtomicInteger readerState : localReadersStateArray) {
      while (readerState != null && readerState.get() == SRWL_STATE_READING) {
        if (System.nanoTime() - lastTime < nanosTimeout) {
          Thread.yield();
        } else {
          // Time has expired and there is at least one ongoing Reader so give up
          stampedLock.asWriteLock().unlock();
          return false;
        }
      }
    }

    return true;
  }
public boolean exclusiveTryLockNanos(long nanosTimeout) throws InterruptedException {
    final long lastTime = System.nanoTime();
    // Try to acquire the lock in write-mode
    if (stampedLock.tryWriteLock(nanosTimeout, TimeUnit.NANOSECONDS) == 0) {
      return false;
    }

    // We can only do this after writerOwner has been set to the current thread
    AtomicInteger[] localReadersStateArray = readersStateArrayRef.get();
    if (localReadersStateArray == null) {
      // Set to dummyArray before scanning the readersStateList to impose
      // a linearizability condition
      readersStateArrayRef.set(dummyArray);
      // Copy readersStateList to an array
      localReadersStateArray = readersStateList.toArray(new AtomicInteger[readersStateList.size()]);
      readersStateArrayRef.compareAndSet(dummyArray, localReadersStateArray);
    }

    // Scan the array of Reader states
    for (AtomicInteger readerState : localReadersStateArray) {
      while (readerState != null && readerState.get() == SRWL_STATE_READING) {
        if (System.nanoTime() - lastTime < nanosTimeout) {
          Thread.yield();
        } else {
          // Time has expired and there is at least one ongoing Reader so give up
          stampedLock.asWriteLock().unlock();
          return false;
        }
      }
    }

    return true;
  }
public boolean exclusiveTryLockNanos ( long nanosTimeout ) throws InterruptedException { final long lastTime = System . nanoTime ( ) ; // Try to acquire the lock in write-mode if ( stampedLock . tryWriteLock ( nanosTimeout , TimeUnit . NANOSECONDS ) == 0 ) { return false ; } // We can only do this after writerOwner has been set to the current thread AtomicInteger [ ] localReadersStateArray = readersStateArrayRef . get ( ) ; if ( localReadersStateArray == null ) { // Set to dummyArray before scanning the readersStateList to impose // a linearizability condition readersStateArrayRef . set ( dummyArray ) ; // Copy readersStateList to an array localReadersStateArray = readersStateList . toArray ( new AtomicInteger [ readersStateList . size ( ) ] ) ; readersStateArrayRef . compareAndSet ( dummyArray , localReadersStateArray ) ; } // Scan the array of Reader states for ( AtomicInteger readerState : localReadersStateArray ) { while ( readerState != null && readerState . get ( ) == SRWL_STATE_READING ) { if ( System . nanoTime ( ) - lastTime < nanosTimeout ) { Thread . yield ( ) ; } else { // Time has expired and there is at least one ongoing Reader so give up stampedLock . asWriteLock ( ) . unlock ( ) ; return false ; } } } return true ; }
Acquires the write lock if it is not held by another thread
within the given waiting time.
<p>Acquires the write lock if the write lock is not
held by another thread and returns immediately with
the value {@code true} if and only if no other thread is attempting a
read lock, setting the write lock {@code reentrantWriterCount}
to one. If another thread is attempting a read lock, this
function <b>may yield until the read lock is released</b>.
<p>If the current thread already holds this lock then the
{@code reentrantWriterCount} is incremented by one and the method returns
{@code true}.
<p>If the write lock is held by another thread then the current
thread yields and lies dormant until one of two things happens:
<ul>
<li>The write lock is acquired by the current thread; or
<li>The specified waiting time elapses
</ul>
<p>If the write lock is acquired then the value {@code true} is
returned and the write lock {@code reentrantWriterCount} is set to one.

@param nanosTimeout the time to wait for the write lock in nanoseconds

@return {@code true} if the lock was free and was acquired
by the current thread, or the write lock was already held by the
current thread; and {@code false} if the waiting time
elapsed before the lock could be acquired.
Acquires the write lock if it is not held by another thread within the given waiting time . <p > Acquires the write lock if the write lock is not held by another thread and returns immediately with the value { @code true } if and only if no other thread is attempting a read lock setting the write lock { @code reentrantWriterCount } to one . If another thread is attempting a read lock this function <b > may yield until the read lock is released< / b > . <p > If the current thread already holds this lock then the { @code reentrantWriterCount } is incremented by one and the method returns { @code true } . <p > If the write lock is held by another thread then the current thread yields and lies dormant until one of two things happens : <ul > <li > The write lock is acquired by the current thread ; or <li > The specified waiting time elapses < / ul > <p > If the write lock is acquired then the value { @code true } is returned and the write lock { @code reentrantWriterCount } is set to one .