List of usage examples for java.util.concurrent.locks LockSupport parkNanos
public static void parkNanos(Object blocker, long nanos)
From source file:io.druid.query.lookup.LookupReferencesManager.java
@LifecycleStart public void start() { if (!lifecycleLock.canStart()) { throw new ISE("can't start."); }//from www . ja v a 2s . co m try { LOG.info("LookupReferencesManager is starting."); loadAllLookupsAndInitStateRef(); if (!testMode) { mainThread = Execs.makeThread("LookupReferencesManager-MainThread", () -> { try { if (!lifecycleLock.awaitStarted()) { LOG.error("WTF! lifecycle not started, lookup update notices will not be handled."); return; } while (!Thread.interrupted() && lifecycleLock.awaitStarted(1, TimeUnit.MILLISECONDS)) { try { handlePendingNotices(); LockSupport.parkNanos(LookupReferencesManager.this, TimeUnit.MINUTES.toNanos(1)); } catch (Throwable t) { LOG.makeAlert(t, "Error occured while lookup notice handling.").emit(); } } } catch (Throwable t) { LOG.error(t, "Error while waiting for lifecycle start. lookup updates notices will not be handled"); } finally { LOG.info("Lookup Management loop exited, Lookup notices are not handled anymore."); } }, true); mainThread.start(); } LOG.info("LookupReferencesManager is started."); lifecycleLock.started(); } finally { lifecycleLock.exitStart(); } }
From source file:io.requery.android.database.sqlite.SQLiteConnectionPool.java
private SQLiteConnection waitForConnection(String sql, int connectionFlags, CancellationSignal cancellationSignal) { final boolean wantPrimaryConnection = (connectionFlags & CONNECTION_FLAG_PRIMARY_CONNECTION_AFFINITY) != 0; final ConnectionWaiter waiter; final int nonce; synchronized (mLock) { throwIfClosedLocked();/* w w w . j a v a2 s.co m*/ // Abort if canceled. if (cancellationSignal != null) { cancellationSignal.throwIfCanceled(); } // Try to acquire a connection. SQLiteConnection connection = null; if (!wantPrimaryConnection) { connection = tryAcquireNonPrimaryConnectionLocked(sql, connectionFlags); // might throw } if (connection == null) { connection = tryAcquirePrimaryConnectionLocked(connectionFlags); // might throw } if (connection != null) { return connection; } // No connections available. Enqueue a waiter in priority order. final int priority = getPriority(connectionFlags); final long startTime = SystemClock.uptimeMillis(); waiter = obtainConnectionWaiterLocked(Thread.currentThread(), startTime, priority, wantPrimaryConnection, sql, connectionFlags); ConnectionWaiter predecessor = null; ConnectionWaiter successor = mConnectionWaiterQueue; while (successor != null) { if (priority > successor.mPriority) { waiter.mNext = successor; break; } predecessor = successor; successor = successor.mNext; } if (predecessor != null) { predecessor.mNext = waiter; } else { mConnectionWaiterQueue = waiter; } nonce = waiter.mNonce; } // Set up the cancellation listener. if (cancellationSignal != null) { cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() { @Override public void onCancel() { synchronized (mLock) { if (waiter.mNonce == nonce) { cancelConnectionWaiterLocked(waiter); } } } }); } try { // Park the thread until a connection is assigned or the pool is closed. // Rethrow an exception from the wait, if we got one. long busyTimeoutMillis = CONNECTION_POOL_BUSY_MILLIS; long nextBusyTimeoutTime = waiter.mStartTime + busyTimeoutMillis; for (;;) { // Detect and recover from connection leaks. if (mConnectionLeaked.compareAndSet(true, false)) { synchronized (mLock) { wakeConnectionWaitersLocked(); } } // Wait to be unparked (may already have happened), a timeout, or interruption. LockSupport.parkNanos(this, busyTimeoutMillis * 1000000L); // Clear the interrupted flag, just in case. Thread.interrupted(); // Check whether we are done waiting yet. synchronized (mLock) { throwIfClosedLocked(); final SQLiteConnection connection = waiter.mAssignedConnection; final RuntimeException ex = waiter.mException; if (connection != null || ex != null) { recycleConnectionWaiterLocked(waiter); if (connection != null) { return connection; } throw ex; // rethrow! } final long now = SystemClock.uptimeMillis(); if (now < nextBusyTimeoutTime) { busyTimeoutMillis = now - nextBusyTimeoutTime; } else { logConnectionPoolBusyLocked(now - waiter.mStartTime, connectionFlags); busyTimeoutMillis = CONNECTION_POOL_BUSY_MILLIS; nextBusyTimeoutTime = now + busyTimeoutMillis; } } } } finally { // Remove the cancellation listener. if (cancellationSignal != null) { cancellationSignal.setOnCancelListener(null); } } }
From source file:LinkedTransferQueue.java
/** * Spins/yields/blocks until node s is matched or caller gives up. * * @param s the waiting node/* w w w . jav a2 s .c o m*/ * @param pred the predecessor of s, or s itself if it has no * predecessor, or null if unknown (the null case does not occur * in any current calls but may in possible future extensions) * @param e the comparison value for checking match * @param timed if true, wait only until timeout elapses * @param nanos timeout in nanosecs, used only if timed is true * @return matched item, or e if unmatched on interrupt or timeout */ private E awaitMatch(Node s, Node pred, E e, boolean timed, long nanos) { long lastTime = timed ? System.nanoTime() : 0L; Thread w = Thread.currentThread(); int spins = -1; // initialized after first item and cancel checks ThreadLocalRandom randomYields = null; // bound if needed for (;;) { Object item = s.item; if (item != e) { // matched assert item != s; s.forgetContents(); // avoid garbage return this.<E>cast(item); } if ((w.isInterrupted() || (timed && nanos <= 0)) && s.casItem(e, s)) { // cancel unsplice(pred, s); return e; } if (spins < 0) { // establish spins at/near front if ((spins = spinsFor(pred, s.isData)) > 0) randomYields = ThreadLocalRandom.current(); } else if (spins > 0) { // spin --spins; if (randomYields.nextInt(CHAINED_SPINS) == 0) Thread.yield(); // occasionally yield } else if (s.waiter == null) { s.waiter = w; // request unpark then recheck } else if (timed) { long now = System.nanoTime(); if ((nanos -= now - lastTime) > 0) LockSupport.parkNanos(this, nanos); lastTime = now; } else { LockSupport.park(this); } } }