Java带缓存的单向链表-线程安全

开发原因

ArrayList太重,线程还不安全

在一些队列处理的时候,ArrayList有点麻烦,还比较占内存,还没缓存,又得再加个缓存队列

所以就开发了这个

轻量级,线程安全,带缓存!nice!!

源码

java 复制代码
package com.geom.queue;

public class SinglyLinked<T>
{
	private Object _lock = new Object();
	
	private int _length = 0;
	
	private SinglyLinkedNode<T> _head = new SinglyLinkedNode<T>();
	private SinglyLinkedNode<T> _end = null;
	
	private int _cacheMax = 0;
	private SinglyLinked<Object> _cache = null;
	
	public SinglyLinked()
	{
	}
	
	/**
	 * 0表示不缓存SinglyLinkedNode
	 * @param cacheMax
	 */
	public SinglyLinked(int cacheMax)
	{
		_cacheMax = cacheMax;
		if(_cacheMax > 0)
		{
			_cache = new SinglyLinked<Object>();
		}
	}
	
	public int length()
	{
		return _length;
	}
	
	@SuppressWarnings("unchecked")
	public void push( T value )
	{
		SinglyLinkedNode<T> node = null;
		if( _cache != null )
		{
			Object obj = _cache.pop();
			if( obj != null ) node = (SinglyLinkedNode<T>)obj;
		}
		if( node==null ) node = new SinglyLinkedNode<T>();
		node.value = value;
		node.next = null;
		
		synchronized (_lock)
		{
			if(_end==null)
				_head.next = node;
			else _end.next = node;
			
			_end = node;
			_length++;
		}
	}
	
	public T pop()
	{
		synchronized (_lock)
		{
			if( _head.next != null )
			{
				T v = _head.next.value;
				if( _head.next==_end )
				{
					_end = null; //防止尾巴自己成一个链表
				}
				if( _cache != null )
				{
					if( _cache._length < _cacheMax )
					{
						_head.next.value = null;
						_cache.push(_head.next);
					}
				}
				_head.next = _head.next.next;

				_length--;
				
				return v;
			}
		}
		return null;
	}
	
	private SinglyLinkedNode<T> _current = null;
	public void resetFor()
	{
		synchronized (_lock)
		{
			_current = _head.next;
		}
	}
	
	/**
	 * 跟resetFor一起使用
	 * @return
	 */
	public T next()
	{
		synchronized (_lock)
		{
			if( _current != null )
			{
				T v = _current.value;
				_current = _current.next;
				
				return v;
			}
		}
		return null;
	}
	
	private static class SinglyLinkedNode<T>
	{
		SinglyLinkedNode<T> next = null;
		public T value = null;
		
		public SinglyLinkedNode()
		{
		}
	}
}

测试代码

java 复制代码
public static void main(String[] args)
{
	SinglyLinked<Integer> link = new SinglyLinked<Integer>(10);
	
	link.push(1);
	link.push(2);
	
	link.resetFor();
	Integer v;
	
	while( (v = link.next()) != null )
	{
		System.out.println(v);
	}
	
	new Thread(new Runnable()
	{
		private int _v = 0;
		@Override
		public void run()
		{
			while(true)
			{
				link.push(_v++);
				try
				{
					Thread.sleep(1);
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
	}).start();
	
	new Thread(new Runnable()
	{
		@Override
		public void run()
		{
			while(true)
			{
				System.out.println(link.length() + ": " + link.pop());
				try
				{
					Thread.sleep(1);
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
	}).start();
}
相关推荐
金銀銅鐵10 分钟前
[Java] 用 GUI 展示 ArrayList 扩容的过程
java·后端·ai编程
磊 子12 分钟前
什么是虚假唤醒(spurious wakeup)?从 POSIX 标准讲到 futex 内核
java·开发语言
飘然生23 分钟前
traceId链路 skywalking java agent
java·开发语言·skywalking
IT研究室29 分钟前
最新计算机毕业设计选题推荐-基于spring boot的母婴全程服务管理系统-网站-文档指导-Java-springboot
java·spring boot·课程设计
CRMEB38 分钟前
商品 SKU 设计,从简单规格到多规格踩过什么
java·前端·数据库·开源
霸道流氓气质39 分钟前
基于 Spring @Profile 的 MQ 消费者按需开关设计
java·后端·spring
hweiyu0042 分钟前
Redis命令:RPOPLPUSH
redis·缓存
silvamux_1 小时前
4款新模型商务邮件翻译横评:0.98到0.46
android·java·javascript
霸道流氓气质1 小时前
Redisson BloomFilter:分布式位图实现、误判率调优与缓存穿透三级防护的 Spring Boot 生产级实战
spring boot·分布式·缓存
Dovis(誓平步青云)1 小时前
车辆进入隧道后信号断开,座舱旅程怎样补回事件
java·开发语言·前端·数据库·电脑