對象池common-pool2源碼分析之對象狀態

對象池common-pool2源碼分析
前端

對象池common-pool2策略分析
java

從前兩篇的分析中能夠看出對象池中的對象會被激活,鈍化,銷燬等,那麼作這些操做的對象須要知足什麼條件呢,與這些操做以後相對應的對象的狀態是什麼樣的呢?
app

首先從PooledObjectState開始分析ide

public enum PooledObjectState {
    /**
     * In the queue, not in use.
     */
    IDLE,

    /**
     * In use.
     */
    ALLOCATED,

    /**
     * In the queue, currently being tested for possible eviction.
     */
    EVICTION,

    /**
     * Not in the queue, currently being tested for possible eviction. An
     * attempt to borrow the object was made while being tested which removed it
     * from the queue. It should be returned to the head of the queue once
     * eviction testing completes.
     * TODO: Consider allocating object and ignoring the result of the eviction
     *       test.
     */
    EVICTION_RETURN_TO_HEAD,

    /**
     * In the queue, currently being validated.
     */
    VALIDATION,

    /**
     * Not in queue, currently being validated. The object was borrowed while
     * being validated and since testOnBorrow was configured, it was removed
     * from the queue and pre-allocated. It should be allocated once validation
     * completes.
     */
    VALIDATION_PREALLOCATED,

    /**
     * Not in queue, currently being validated. An attempt to borrow the object
     * was made while previously being tested for eviction which removed it from
     * the queue. It should be returned to the head of the queue once validation
     * completes.
     */
    VALIDATION_RETURN_TO_HEAD,

    /**
     * Failed maintenance (e.g. eviction test or validation) and will be / has
     * been destroyed
     */
    INVALID,

    /**
     * Deemed abandoned, to be invalidated.
     */
    ABANDONED,

    /**
     * Returning to the pool.
     */
    RETURNING
}

以上枚舉定義了對象能夠出現的全部狀態:源碼分析

IDLE,空閒,在隊列中;測試

ALLOCATED:在使用;this

EVICTION:在隊列中,正在測試中,可能會被驅逐;spa

EVICTION_RETURN_TO_HEAD:不是在隊列中,正在測試中,可能會被驅逐.當驅逐測試結束的時候,它應該返回隊列的前端;.net

VALIDATION:在隊列中,正在校驗;code

VALIDATION_PREALLOCATED:不在隊列中,使用前進行校驗;

VALIDATION_RETURN_TO_HEAD:不在隊列中,正在校驗,校驗結束後應該返回隊列的前端;

INVALID:無效的;

ABANDONED:廢棄的;

RETURNING:還回對象池.

池對象狀態的改變發生在使用,還回,激活等操做的時候,即在調用GenericObjectPool內相應的方法時發生改變.

DefaultPooledObject的方法裏能更清晰的看到狀態的變化.

對象建立後默認的狀態是空閒

private PooledObjectState state = PooledObjectState.IDLE;

開始驅逐前的測試,狀態由空閒變爲測試中

public synchronized boolean startEvictionTest() {
    if (state == PooledObjectState.IDLE) {
        state = PooledObjectState.EVICTION;
        return true;
    }

    return false;
}

測試結束,由測試中變爲空閒

public synchronized boolean endEvictionTest(
        Deque<PooledObject<T>> idleQueue) {
    if (state == PooledObjectState.EVICTION) {
        state = PooledObjectState.IDLE;
        return true;
    } else if (state == PooledObjectState.EVICTION_RETURN_TO_HEAD) {
        state = PooledObjectState.IDLE;
        if (!idleQueue.offerFirst(this)) {
            // TODO - Should never happen
        }
    }

    return false;
}

由空閒變爲在使用,或由在隊列中正在進行測試變爲不在隊列中正在測試

public synchronized boolean allocate() {
    if (state == PooledObjectState.IDLE) {
        state = PooledObjectState.ALLOCATED;
        lastBorrowTime = System.currentTimeMillis();
        lastUseTime = lastBorrowTime;
        borrowedCount++;
        if (logAbandoned) {
            borrowedBy = new AbandonedObjectCreatedException();
        }
        return true;
    } else if (state == PooledObjectState.EVICTION) {
        // TODO Allocate anyway and ignore eviction test
        state = PooledObjectState.EVICTION_RETURN_TO_HEAD;
        return false;
    }
    // TODO if validating and testOnBorrow == true then pre-allocate for
    // performance
    return false;
}

由在使用變爲歸還

public synchronized boolean deallocate() {
    if (state == PooledObjectState.ALLOCATED ||
            state == PooledObjectState.RETURNING) {
        state = PooledObjectState.IDLE;
        lastReturnTime = System.currentTimeMillis();
        borrowedBy = null;
        return true;
    }

    return false;
}

無效的

public synchronized void invalidate() {
    state = PooledObjectState.INVALID;
}
相關文章
相關標籤/搜索