JAVA写同步栈_tomcat实现的同步队列和同步栈
tomcat实现的同步队列,同步栈用于数据量比较固定且基本很少删除的场景,尽可能减少内存消耗。
同步队列
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.collections;
/**
* This is intended as a (mostly) GC-free alternative to
* {@link java.util.concurrent.ConcurrentLinkedQueue} when the requirement is to
* create an unbounded queue with no requirement to shrink the queue. The aim is
* to provide the bare minimum of required functionality as quickly as possible
* with minimum garbage.
*
* @param The type of object managed by this queue
*/
public class SynchronizedQueue {
public static final int DEFAULT_SIZE = 128;
private Object[] queue;
private int size;
private int insert = 0;
private int remove = 0;
public SynchronizedQueue() {
this(DEFAULT_SIZE);
}
public SynchronizedQueue(int initialSize) {
queue = new Object[initialSize];
size = initialSize;
}
public synchronized boolean offer(T t) {
queue[insert++] = t;
// Wrap
if (insert == size) {
insert = 0;
}
if (insert == remove) {
expand();
}
return true;
}
public synchronized T poll() {
if (insert == remove) {
// empty
return null;
}
@SuppressWarnings("unchecked")
T result = (T) queue[remove];
queue[remove] = null;
remove++;
// Wrap
if (remove == size) {
remove = 0;
}
return result;
}
private void expand() {
int newSize = size * 2;
Object[] newQueue = new Object[newSize];
System.arraycopy(queue, insert, newQueue, 0, size - insert);
System.arraycopy(queue, 0, newQueue, size - insert, insert);
insert = size;
remove = 0;
queue = newQueue;
size = newSize;
}
public synchronized int size() {
int result = insert - remove;
if (result < 0) {
result += size;
}
return result;
}
public synchronized void clear() {
queue = new Object[size];
insert = 0;
remove = 0;
}
}
同步栈
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.collections;
/**
* This is intended as a (mostly) GC-free alternative to
* {@link java.util.concurrent.ConcurrentLinkedQueue} when the requirement is to
* create a pool of re-usable objects with no requirement to shrink the pool.
* The aim is to provide the bare minimum of required functionality as quickly
* as possible with minimum garbage.
*
* @param The type of object managed by this stack
*/
public class SynchronizedStack {
public static final int DEFAULT_SIZE = 128;
private static final int DEFAULT_LIMIT = -1;
private int size;
private final int limit;
/*
* Points to the next available object in the stack
*/
private int index = -1;
private Object[] stack;
public SynchronizedStack() {
this(DEFAULT_SIZE, DEFAULT_LIMIT);
}
public SynchronizedStack(int size, int limit) {
if (limit > -1 && size > limit) {
this.size = limit;
} else {
this.size = size;
}
this.limit = limit;
stack = new Object[size];
}
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;
}
@SuppressWarnings("unchecked")
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;
}
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;
}
}
JAVA写同步栈_tomcat实现的同步队列和同步栈相关推荐
- Java API(十五):Queue队列、Deque栈、Map集合
文章目录 Java API 一.Queue队列 1.Queue接口及LinkedList实现 2.Queue常用方法 二.Deque栈 1.Deque接口及LinkedList实现 2.Deque常用 ...
- 两个栈实现一个队列/两个队列实现一个栈
http://blog.csdn.net/sinat_30472685/article/details/70157227 1两个栈实现一个队列 1.原理分析: 队列的主要操作有两个:入队操作和出队操作 ...
- python ——两个队列实现一个栈两个栈实现一个队列
1.两个队列实现一个栈 进栈:元素入队列A 出栈:判断如果队列A只有一个元素,则直接出队.否则,把队A中的元素出队并入队B,直到队A中只有一个元素,再直接出队.为了下一次继续操作,互换队A和队B. p ...
- java标量替换_JAVA逃逸分析、栈上分配、标量替换、同步消除
一.逃逸分析 逃逸分析是编译语言中的一种优化分析,而不是一种优化的手段.通过对象的作用范围的分析,为其他优化手段提供分析数据从而进行优化. 逃逸分析包括: 全局变量赋值逃逸 方法返回值逃逸 实例引用发 ...
- 用Java写LRC制作器,实现音乐播放和滑块条进度同步
最近两天,我第一次尝试用Java写一个GUI程序,写了个LRC文件编辑器.简单地说,就是先导入歌,然后导入歌词(顺序可以颠倒,导入歌词可以用复制粘贴代替),然后一边播放歌曲,一边添加时间标签.然后根据 ...
- 异构数据库数据同步工具DataX教程,安装、数据同步、java执行
前言 DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.Oracle.SqlServer.Postgre.HDFS.Hive.ADS.HBase.TableStor ...
- java控制一次传10条数据_java 定时同步数据的任务优化
前言 定时任务在系统中并不少见,主要目的是用于需要定时处理数据或者执行某个操作的情况下,如定时关闭订单,或者定时备份.而常见的定时任务分为2种,第一种:固定时间执行,如:每分钟执行一次,每天执行一次. ...
- Java微信公众号开发之微信粉丝信息一键同步
1.前言 在公众号开发的过程中,一般都需要获取粉丝资料,针对单个粉丝,我们可以通过openid获取其粉丝信息: 但不排除这种业务,比如目前开发的公众号已经在使用中,,当前的框架或者功能已经不能够满足用 ...
- java 两个线程同步_Java 多线程(二)—— 线程的同步
实现Runnable接口 public classTestThread2 {public static voidmain(String [] args){ Window window=newWindo ...
最新文章
- powerbi 线性回归_Power BI二月新增图表及课程福利
- Sharepoint学习笔记---Linq to Sharepoint--查询语法
- java web的友好页面_JavaWeb 之 由 Tomcat 展示友好的错误信息页面
- JAVA常见命名规范
- 几款查看dll和exe信息的小工具
- 计算机通过注册表修改摄像机设备的名称
- android SD卡文件夹
- Libfetion在Ubuntu下的中文输出了局
- android系统目录哪些不能删除,手机系统文件中的以下文件是不能删除
- linux配置路由器命令手册,Linux配置路由器
- 【行业视角】是什么让元宇宙土地与房产变得有价值
- WARNING: The converted filename is an ASM fully qualified filename.
- win7笔记本做wifi热点
- 单片机STM8S测量电压电路_纸张计数测量显示装置+【2019年电赛F题国一获奖作品】...
- 资深Android开发带你入门Framework,深夜思考
- Mifare S50控制条件
- String类的trim()方法之不能消除的空格
- 惊喜!网易自媒体一星开通收益!
- 最全英语单词下载地址
- 微软、谷歌、百度等公司经典面试100题[第101-160题]
热门文章
- 不同文件类型输出及ContentType表
- android默认代码混淆,Android SDK默认混淆配置文件
- linux6.8安装图形桌面,图形/文本界面安装CentOS 6.8系统详解
- DO、DTO、BO、AO、VO、POJO定义
- dedecms更改php目录名称,dedecms修改专题目录名称(路径)
- 如何快速理解读懂他人代码(下)——技巧学习篇
- 20155220 吴思其 2016-2017《java程序设计》第一周总结
- 贷款购房 房贷 每月还款额是如何计算出来的? 每月还款额计算步骤/方法
- ViewGroup之getScrollX()
- 关于客户端无法获得服务器端GP服务(Geoprocessing Service)结果的解决办法