/** * Stops the executor thread */ public void stopExecutionThread() { if (stampedLock.isWriteLocked()) { // already stopped return; } // lock and never free in order to prevent future queueing stampedLock.writeLock(); /* * Because of concurrent queue follow first-in first-out semantic, *we have guarantee that POISON will be a last task in the QUEUE. */ taskQueue.add(POISON); LockSupport.unpark(workerThread); }
/** * Stops the executor thread */ public void stopExecutionThread() { if (stampedLock.isWriteLocked()) { // already stopped return; } // lock and never free in order to prevent future queueing stampedLock.writeLock(); /* * Because of concurrent queue follow first-in first-out semantic, *we have guarantee that POISON will be a last task in the QUEUE. */ taskQueue.add(POISON); LockSupport.unpark(workerThread); }
/** * Attempts to release the write lock. * <p>If the current thread is the holder of this lock then * the {@code reentrantWriterCount} is decremented. If {@code * reentrantWriterCount} is now zero then the lock is released. * If the current thread is not the holder of this lock then {@link * IllegalMonitorStateException} is thrown. * * @throws IllegalMonitorStateException if the current thread does not * hold this lock. */ public void exclusiveUnlock() { if (!stampedLock.isWriteLocked()) { // ERROR: tried to unlock a non write-locked instance throw new IllegalMonitorStateException(); } stampedLock.asWriteLock().unlock(); }
if (!stampedLock.isWriteLocked()) { while (stampedLock.isWriteLocked()) { Thread.yield();
/** * Acquires the read lock only if the write lock is not held by * another thread at the time of invocation. * <p>Acquires the read lock if the write lock is not held by * another thread and returns immediately with the value * {@code true}. * <p>If the write lock is held by another thread then * this method will return immediately with the value * {@code false}. * * @return {@code true} if the read lock was acquired */ public boolean sharedTryLock() { ReadersEntry localEntry = entry.get(); // Initialize a new Reader-state for this thread if needed if (localEntry == null) { localEntry = addState(); } final AtomicInteger currentReadersState = localEntry.state; currentReadersState.set(SRWL_STATE_READING); if (!stampedLock.isWriteLocked()) { // Acquired lock in read-only mode return true; } else { // Go back to SRWL_STATE_NOT_READING and quit currentReadersState.set(SRWL_STATE_NOT_READING); return false; } }
while (true) { currentReadersState.set(SRWL_STATE_READING); if (!stampedLock.isWriteLocked()) {