Example usage for java.util.concurrent.locks LockSupport unpark

List of usage examples for java.util.concurrent.locks LockSupport unpark

Introduction

In this page you can find the example usage for java.util.concurrent.locks LockSupport unpark.

Prototype

public static void unpark(Thread thread) 

Source Link

Document

Makes available the permit for the given thread, if it was not already available.

Usage

From source file:io.requery.android.database.sqlite.SQLiteConnectionPool.java

private void wakeConnectionWaitersLocked() {
    // Unpark all waiters that have requests that we can fulfill.
    // This method is designed to not throw runtime exceptions, although we might send
    // a waiter an exception for it to rethrow.
    ConnectionWaiter predecessor = null;
    ConnectionWaiter waiter = mConnectionWaiterQueue;
    boolean primaryConnectionNotAvailable = false;
    boolean nonPrimaryConnectionNotAvailable = false;
    while (waiter != null) {
        boolean unpark = false;
        if (!mIsOpen) {
            unpark = true;// w w w  .  j  a v  a2  s . c  o  m
        } else {
            try {
                SQLiteConnection connection = null;
                if (!waiter.mWantPrimaryConnection && !nonPrimaryConnectionNotAvailable) {
                    connection = tryAcquireNonPrimaryConnectionLocked(waiter.mSql, waiter.mConnectionFlags); // might throw
                    if (connection == null) {
                        nonPrimaryConnectionNotAvailable = true;
                    }
                }
                if (connection == null && !primaryConnectionNotAvailable) {
                    connection = tryAcquirePrimaryConnectionLocked(waiter.mConnectionFlags); // might throw
                    if (connection == null) {
                        primaryConnectionNotAvailable = true;
                    }
                }
                if (connection != null) {
                    waiter.mAssignedConnection = connection;
                    unpark = true;
                } else if (nonPrimaryConnectionNotAvailable && primaryConnectionNotAvailable) {
                    // There are no connections available and the pool is still open.
                    // We cannot fulfill any more connection requests, so stop here.
                    break;
                }
            } catch (RuntimeException ex) {
                // Let the waiter handle the exception from acquiring a connection.
                waiter.mException = ex;
                unpark = true;
            }
        }

        final ConnectionWaiter successor = waiter.mNext;
        if (unpark) {
            if (predecessor != null) {
                predecessor.mNext = successor;
            } else {
                mConnectionWaiterQueue = successor;
            }
            waiter.mNext = null;

            LockSupport.unpark(waiter.mThread);
        } else {
            predecessor = waiter;
        }
        waiter = successor;
    }
}