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;

}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/431839.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IOS高级编程之二:IOS的数据存储与IO

一、应用程序沙盒 IOS应用程序职能在系统为该应用所分配的文件区域下读写文件&#xff0c;这个文件区域就是应用程序沙盒。所有的非代码文件如&#xff1a;图片、声音、映象等等都存放在此。 在mac中command&#xff0b;shift&#xff0b;G命令&#xff0c;然后输入users/用户名…

安卓投屏大师_苹果,安卓手机如何免费投屏?只要悄悄按下这里,便能轻松实现...

现在很多手机都有自带投屏功能&#xff0c;这样一来我们便可以将所看的视频&#xff0c;所玩的游戏投屏到电脑或电视上了&#xff0c;当然也需要这些设备支持投屏才行。一、无线投屏1、苹果手机苹果手机的投屏功能在哪里呢&#xff1f;只要打开苹果手机从下往上滑动&#xff0c…

java 反射解析xml_java反射获取xml元素

类名:class Person {public void run(String who){System.out.println("Person::run()" who);}public void jump(String who){System.out.println("Person::jump()" who);}public void run(){System.out.println("Person::run()");}public voi…

怎样的中奖算法能让人信服

话说写一个抽奖程序还不容易&#xff0c;不就是生成一个随机数吗&#xff0c;哪需什么算法之类的。 从技术上说&#xff0c;这确实不难。事实上&#xff0c;你怎么写都可以&#xff0c;因为程序只运行在特定的设备上&#xff0c;外人根本无法了解其中的细节。 那么问题就来了&a…

b站电脑客户端_如何将B站的flv格式的视频转换成mp4格式

经常看到B站有精彩的视频片段&#xff0c;于是想把这些视频下载保存到电脑&#xff0c;但是发现没有下载按钮&#xff0c;是不是很悲催。有些时候想从优酷、土豆网这些视频网站下载视频&#xff0c;结果却提示要先下载视频客户端才能继续下载视频&#xff0c;运气差的话&#x…

linux Packet socket (1)简单介绍

本文主要来自于linux自带的man packet手冊&#xff1a; http://man7.org/linux/man-pages/man7/packet.7.html 平时常常使用的INET套接字提供的是7层的抓包能力&#xff0c;抓上来的data直接就是tcp或者udp的payload&#xff0c;无需关心L3和L4的头部信息。 Packet套接字提供的…

asp.net 设置 excel alignment_教你如何用Python轻轻松松操作Excel、Word、CSV,一文就够了,赶紧码住!!!...

作者&#xff1a;奈何缘浅wyjhttps://juejin.im/post/6868073137263607821Python 操作 Excel常用工具数据处理是 Python 的一大应用场景&#xff0c;而 Excel 又是当前最流行的数据处理软件。因此用 Python 进行数据处理时&#xff0c;很容易会和 Excel 打起交道。得益于前人的…

java 页面输出一个页面_java学习之:一个完整页面输出信息的过程(以输出Doctor表中信息为例)...

最近在练习java程序&#xff0c;总结一下从数据库查询信息并输出到jsp页面的过程。主要数据处理在src.cn.javatest包下面项目预览1&#xff0c;配置项目根目录src目录下的druid.properties数据库信息(相当于一个数据库配置文件)里面的信息可以在下载druid中获得&#xff0c;只需…

[xsd学习]xsd介绍

一直以来项目中对xml格式的判断使用的都是dtd格式&#xff0c;直到最近才发现&#xff0c;不知何时都已经转为xsd来进行判断和校验&#xff0c;于是今天专门找资料看下&#xff0c;不得不说&#xff0c;对于这类资料的入门&#xff0c;w3cschool真是个不错的资料库&#xff0c;…

教学目标四个维度_【深度好文】体育教案中的教学目标与学习目标应如何表述...

体育教师大本营教学/训练/职业/成长强 烈 建 议 大 家 星 标 我 们教 学 路 上 ☆ 不 离 不 弃在以往看到的体育课的教案上&#xff0c;目标部分不是用教学目标就是用学习目标来表述&#xff0c;然而&#xff0c;这两种目标表达形式有没有本质区别&#xff1f;分别该如何表述…

java中检查性异常类_Java异常处理、java语言推崇使用检查类型异常

异常处理是java语言的重要特性之一&#xff0c;《Three Rules for effective Exception Handling》一文中是这么解释的&#xff1a;它主要帮助我们在debug的过程中解决下面的三个问题。什么出错了哪里出错了为什么出错java语言可以说是提供了过于完善的异常处理机制&#xff0c…

why I need a flow learn note.

1.其实学网站学安卓也写了txt的学习笔记。然后扔着&#xff0c;&#xff08;没有时间线和互动性&#xff09;&#xff0c;然后就没有然后了。 2.知识体系&#xff0c;一个月后&#xff0c;哎呦我擦&#xff0c;我会啥来着&#xff08;搞了这么久&#xff0c;深有体会&#xff0…

nodejs readfilesync 路径_Linux 磁盘多路径聚合multipath

在日常工作中我们经常遇到配置存储的多路径聚合。多路径的目的是&#xff0c;当主机HBA卡、线缆、交换机或者存储设备的控制器故障等原因造成一条物理路径失效时,服务器可以将通过此物理路径的I/O转移到其他正常的物理路径上面,应用程序不会觉察到这种改变&#xff0c;从而提高…

php考勤分析,php考勤系统

【实例简介】自己写的php学生考勤系统&#xff0c;包括开题报告&#xff0c;结题报告&#xff0c;mysql数据库【实例截图】【核心代码】PHPkaoqinsystem└── PHP考勤系统├── database│ └── kaoqin.sql├── kaoqin│ ├── addrd.php│ ├── addstd.php│ …

遥 控 器

acm.zznu.edu.cn/problem.php?id1617 遥 控 器 时间限制: 1 Sec 内存限制: 128 MB提交: 25 解决: 9[提交][状态]题目描述 Dr.Kong 有一台高级电视机&#xff0c;这台电视机可以接受100个频道&#xff08;从0到99编号&#xff09;。电视的配套遥控器有13个按钮&#xff1a; 1…

php7与golang,golang 调用 php7

执行php文件func Test_exec(t *testing.T) {engine.Initialize()ctx : &engine.Context{Output: os.Stdout,}err : engine.RequestStartup(ctx)if err ! nil {fmt.Println(err)}defer engine.RequestShutdown(ctx)err ctx.Exec("/tmp/index.php")if err ! nil {…

u 20ubuntu 安装 postfix_极力推荐和田咨询问题U型钢托盘厂家

12极力推荐和田咨询问题U型钢托盘厂家泊头市毅伽属制品有限公司坐落于河北省泊头市龙华街北4公里&#xff0c;濒临京沪、石黄高速公路以及104、307国道&#xff0c;另有廊泊路贯穿南北交通十分便利。本公司设计生产各种冷弯型钢&#xff0c;产品包括C型钢、Z型钢、U型钢、M型钢…

HTML5中lineCap端点样式遇到closePath()

定义和用法 lineCap 属性设置或返回线条末端线帽的样式。 注释&#xff1a;"round" 和 "square" 会使线条略微变长。 默认值&#xff1a;buttJavaScript 语法&#xff1a;context.lineCap"butt|round|square";属性值 值描述butt默认。向线条的每…

php鼠标悬停显示图片,鼠标滑过出现预览的大图提示效果

当鼠标滑过图片时&#xff0c;图片会出现预览的大图&#xff0c;大图下面还会有介绍文字。.aa{width:88px;height :100px;}$(function () {var x 10;var y 20;$("a.tooltip").mouseover(function (e) {this.myTitle this.title;this.title "";var imgT…

405 not allowed什么意思_二驴质问散打:为什么不救天道!面临一个亿赔款?次惑小仙女宣布与可乐分手!...

次惑小仙女发作品表示&#xff1a;对不起让你们失望了&#xff0c;慢慢也学着长大了,懂得了很多,以前每天就知道天真傻笑的玩,后来懂得了努力和加油,但是很多东西并不是大家看到的那样背后的事情谁又能知道。都认为他对我很好&#xff0c;只有自己知道&#xff0c;呵呵。有些时…