【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
从前两篇的分析中可以看出对象池中的对象会被激活,钝化,销毁等,那么做这些操作的对象需要满足什么条件呢,与这些操作之后相对应的对象的状态是什么样的呢?
首先从PooledObjectState开始分析
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:在使用;
EVICTION:在队列中,正在测试中,可能会被驱逐;
EVICTION_RETURN_TO_HEAD:不是在队列中,正在测试中,可能会被驱逐.当驱逐测试结束的时候,它应该返回队列的前端;
VALIDATION:在队列中,正在校验;
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;
}
来源:oschina
链接:https://my.oschina.net/u/657390/blog/659502