SynchronizedStack -- tomcat8同步栈
2024-08-26 12:27:57
同步栈(安全栈):
org.apache.tomcat.util.collections.SynchronizedStack
通过stack栈锁来控制栈中获取的类T。通过push、pop和clear方法操作栈对象。栈初始化大小是128,没有上限。 初始化:
public SynchronizedStack() {
this(DEFAULT_SIZE:128, DEFAULT_LIMIT:-1);
} 压入栈:
public synchronized boolean push(T obj) {
index++;
if (index == size) {
if (limit == -1 || size < limit) {
expand();
} else {
index--;
return false;
}
}
stack[index] = obj;
return true;
}
private void expand() {
int newSize = size * 2;
if (limit != -1 && newSize > limit) {
newSize = limit;
}
Object[] newStack = new Object[newSize];
System.arraycopy(stack, 0, newStack, 0, size);
// This is the only point where garbage is created by throwing away the
// old array. Note it is only the array, not the contents, that becomes
// garbage.
stack = newStack;
size = newSize;
} 获取栈对象:
public synchronized T pop() {
if (index == -1) {
return null;
}
T result = (T) stack[index];
stack[index--] = null;
return result;
} 清除栈释放资源:
public synchronized void clear() {
if (index > -1) {
for (int i = 0; i < index + 1; i++) {
stack[i] = null;
}
}
index = -1;
}
举例:
最新文章
- 使用Ring Buffer构建高性能的文件写入程序
- php广告图片循环播放 幻灯片效果
- SPRING 标签库
- git/ssh捋不清的几个问题
- eclipse设置及快捷键
- Unsupported configuration attributes: [FILE_UPLOAD]
- 无法启动此程序,因为计算机中丢失MSVCP110.dll
- NYOJ-228 士兵杀敌5
- css选择器nth-child()和nth-of-type()的应用
- win8 任务栏不合并隐藏标题
- iOS真机调试之我见
- CSS复习第一天(简单规范)
- php导出excel数据
- UVA 11388-GCD LCM(数学)
- 办理卡尔加里大学(本科)学历认证『微信171922772』calgary学位证成绩单使馆认证University of calgary
- mysql 入门 基本命令
- unity实现剧情对话
- GitLab push除发Jenkins事件
- OLAP + MDX
- 1.html基础标签:文本+链接+图片