All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zusmart.base.thread.FastThreadLocal Maven / Gradle / Ivy

Go to download

提供基础的工具类及方法类,Logging,Scanner,Buffer,NetWork,Future,Thread

There is a newer version: 1.0.6
Show newest version
package com.zusmart.base.thread;

import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.zusmart.base.activity.support.AbstractAttachable;
import com.zusmart.base.util.GenericUtils;

public class FastThreadLocal extends AbstractAttachable {

	private static final int maxIndexedVarsSize = 32;
	private static final AtomicInteger indexedVarsIndex = new AtomicInteger(0);
	private static final ThreadLocal slowThreadLocal = new ThreadLocal();

	private byte[] bytes32 = new byte[32];
	private Map charsetDecoders = new IdentityHashMap();
	private Map charsetEncoders = new IdentityHashMap();
	private Object[] indexedVariables = new Object[maxIndexedVarsSize];
	private StringBuilder stringBuilder = new StringBuilder(512);

	public byte[] getBytes32() {
		return bytes32;
	}

	public CharsetDecoder getCharsetDecoder(Charset charset) {
		CharsetDecoder decoder = charsetDecoders.get(charset);
		if (decoder == null) {
			decoder = charset.newDecoder();
			this.charsetDecoders.put(charset, decoder);
		}
		return decoder;
	}

	public CharsetEncoder getCharsetEncoder(Charset charset) {
		CharsetEncoder encoder = charsetEncoders.get(charset);
		if (encoder == null) {
			encoder = charset.newEncoder();
			this.charsetEncoders.put(charset, encoder);
		}
		return encoder;
	}

	public Object getIndexedVariable(int index) {
		return this.indexedVariables[index];
	}

	public List getList(int index) {
		List list = GenericUtils.parse(this.getIndexedVariable(index));
		if (list == null) {
			list = new ArrayList();
			this.setIndexedVariable(index, list);
		}
		list.clear();
		return list;
	}

	public Map getMap(int index) {
		Map map = GenericUtils.parse(this.getIndexedVariable(index));
		if (map == null) {
			map = new HashMap();
			this.setIndexedVariable(index, map);
		}
		map.clear();
		return map;
	}

	public StringBuilder getStringBuilder() {
		this.stringBuilder.setLength(0);
		return stringBuilder;
	}

	public void setIndexedVariable(int index, Object value) {
		this.indexedVariables[index] = value;
	}

	public static void destroy() {

	}

	public static FastThreadLocal get() {
		Thread thread = Thread.currentThread();
		if (thread instanceof FastThread) {
			return ((FastThread) thread).getThreadLocal();
		} else {
			FastThreadLocal l = slowThreadLocal.get();
			if (l == null) {
				l = new FastThreadLocal();
				slowThreadLocal.set(l);
			}
			return l;
		}
	}

	public static int nextIndexedVariablesIndex() {
		if (indexedVarsIndex.get() >= maxIndexedVarsSize) {
			return -1;
		}
		int index = indexedVarsIndex.getAndIncrement();
		if (index >= maxIndexedVarsSize) {
			return -1;
		}
		return index;
	}

}