当前位置:首页 > 开发 > 操作系统 > 正文

扩展下Object Pool的设计

发表于: 2013-10-30   作者:cywhoyi   来源:转载   浏览次数:
摘要: 首先需要了解些java并发教程,其实java也是可以通过native的本地方法去操作底层api的,比如大家所知道Aomic、CAS等概念性的操作。 下面一篇文章是来源于CSDN的觉得挺好的讲关于unsafe的 http://blog.csdn.net/aesop_wubo/article/details/7537278 下面是一个重新设计过的对象池的获取 pub
首先需要了解些java并发教程,其实java也是可以通过native的本地方法去操作底层api的,比如大家所知道Aomic、CAS等概念性的操作。

下面一篇文章是来源于CSDN的觉得挺好的讲关于unsafe的

http://blog.csdn.net/aesop_wubo/article/details/7537278

下面是一个重新设计过的对象池的获取

public class FastObjectPool<T> {
	private Holder<T>[] objects;
	private volatile int takePointer;
	private int releasePointer;
	private final int mask;
	private final long BASE;
	private final long INDEXSCALE;
	private final long ASHIFT;
	public ReentrantLock lock = new ReentrantLock();
	private ThreadLocal<Holder<T>> localValue = new ThreadLocal<Holder<T>>();
	@SuppressWarnings("unchecked")
	public FastObjectPool(PoolFactory<T> factory, int size) {

		int newSize = 1;
		while (newSize < size) {
			newSize = newSize << 1;
		}
		size = newSize;
		objects = new Holder[size];
		for (int x = 0; x < size; x++) {
			objects[x] = new Holder<T>(factory.create());
		}
		mask = size - 1;
		releasePointer = size;
		BASE = THE_UNSAFE.arrayBaseOffset(Holder[].class);
		INDEXSCALE = THE_UNSAFE.arrayIndexScale(Holder[].class);
		ASHIFT = 31 - Integer.numberOfLeadingZeros((int) INDEXSCALE);
	}

	public Holder<T> take() {
		int localTakePointer;

		Holder<T> localObject = localValue.get();
		if (localObject != null) {
			if (localObject.state.compareAndSet(Holder.FREE, Holder.USED)) {
				return localObject;
			}
		}

		while (releasePointer != (localTakePointer = takePointer)) {
			int index = localTakePointer & mask;
			Holder<T> holder = objects[index];
			// if(holder!=null && THE_UNSAFE.compareAndSwapObject(objects,
			// (index*INDEXSCALE)+BASE, holder, null))
			if (holder != null
					&& THE_UNSAFE.compareAndSwapObject(objects,
							(index << ASHIFT) + BASE, holder, null)) {
				takePointer = localTakePointer + 1;
				if (holder.state.compareAndSet(Holder.FREE, Holder.USED)) {
					localValue.set(holder);
					return holder;
				}
			}
		}
		return null;
	}

	public void release(Holder<T> object) throws InterruptedException {
		lock.lockInterruptibly();
		try {
			int localValue = releasePointer;
			// long index = ((localValue & mask) * INDEXSCALE ) + BASE;
			long index = ((localValue & mask) << ASHIFT) + BASE;
			if (object.state.compareAndSet(Holder.USED, Holder.FREE)) {
				THE_UNSAFE.putOrderedObject(objects, index, object);
				releasePointer = localValue + 1;
			} else {
				throw new IllegalArgumentException("Invalid reference passed");
			}
		} finally {
			lock.unlock();
		}
	}

	public static class Holder<T> {
		private T value;
		public static final int FREE = 0;
		public static final int USED = 1;

		private AtomicInteger state = new AtomicInteger(FREE);

		public Holder(T value) {
			this.value = value;
		}

		public T getValue() {
			return value;
		}
	}

	public static interface PoolFactory<T> {
		public T create();
	}

	public static final Unsafe THE_UNSAFE;
	static {
		try {
			final PrivilegedExceptionAction<Unsafe> action = new PrivilegedExceptionAction<Unsafe>() {
				public Unsafe run() throws Exception {
					Field theUnsafe = Unsafe.class
							.getDeclaredField("theUnsafe");
					theUnsafe.setAccessible(true);
					return (Unsafe) theUnsafe.get(null);
				}
			};

			THE_UNSAFE = AccessController.doPrivileged(action);
		} catch (Exception e) {
			throw new RuntimeException("Unable to load unsafe", e);
		}
	}
}

扩展下Object Pool的设计

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
SQL Server 2014缓存池扩展 (Buffer Pool Extension)功能可以将缓存池扩展到较快的SSD存储上。为内
线上tomcat服务器报错: 现象: 应用在高峰来临的时候报大量报下面的错误。 [java] view plain copy
这我们得实际开发当中,当遇到并发的应用时,我们总是需要设计一个这样的对象,该对象在多线程下被
Android系统下的SeekBar太简单,简单到连起止的提示和动态选择后变化的提示都没有。最起码我没找到
好吧,我承认取的标题太笼统了,有点哗众取宠的嫌疑了。 首先说明,本篇要讨论的是对FilterSecurity
DAO设计模式简介 DAO的主要功能是数据操作,在程序的标准开发架构中属于数据层的操作。数据访问:顾
1、安装编译器 Objective-C的编译器有很多,其中LLVM属于从GCC发展出来的,主要使用在苹果的平台中
PHP扩展的安装方式通常分为两种: 1. 随同PHP编译 2. 生成单独的.so文件 这里介绍第二种方式,对于
最小连接5,最大连接50,无延迟,排除预热,循环查询“select 1” 线程数 4 12 36 108 324 972 2916
http://celeryq.org/ Overview This is a high level overview of the architecture. he broker deli
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号