java 线程池处理_java线程池处理并发业务

该博客探讨了Java并发编程中如何使用线程池优化多线程处理,以及实现了一个简单的缓存池`CachePool`类,用于存储和管理对象。通过固定大小的线程池避免频繁创建线程,提高性能。同时,展示了在缓存不为空时,如何遍历并处理缓存中的对象,确保操作的正确性和效率。
摘要由CSDN通过智能技术生成

/**

* 缓存对象 map

*/

public static CachePool mapPool = CachePool.getInstance();

private static final int NTHREADS=5;

// 使用线程池来避免 为每个请求创建一个线程。

private static final Executor threadPool=Executors.newFixedThreadPool(NTHREADS);

public FinancesVo buyProduct(FinancesVo financesVo) {

mapPool.put("financesVo",financesVo);

String key = "";

Map param = new HashMap();

synchronized (mapPool){

System.out.println("Thread.currentThread().getName()........................................"+Thread.currentThread().getName());

Iterator it = mapPool.keySet().iterator();

//缓存不为空时,取出一个值

while (it.hasNext())

{

key = (String) it.next();

financesVo = (FinancesVo) mapPool.get(key);

param.put(key, financesVo);

if (null != param){

String resultCode="";

financesVo=sureProduct(financesVo);

if(financesVo.getIsTrue()){

resultCode="success";

}

//为防止重复,将其移除

mapPool.remove(key);

}

}

}

return financesVo;

}

public class CachePool extends AbstractMap{

// 私有化缓存对象实例

private static CachePool cachePool = new CachePool();

private int maxCount = 1000;

private BlockingQueue queue = new LinkedBlockingQueue();

/**

* private Constructor.

* @return

*/

private CachePool() {

}

/**

* 开放一个公有方法,判断是否已经存在实例,有返回,没有新建一个在返回

* @return

*/

public static CachePool getInstance(){

return cachePool;

}

/**

* The Entry for this Map.

* @author AnCan

*

*/

private class Entry implements Map.Entry{

private Key key;

private Value value;

public Entry(Key key, Value value){

this.key = key;

this.value = value;

}

@Override

public String toString() {

return key + "=" + value;

}

public Key getKey() {

return key;

}

public Value getValue() {

return value;

}

public Value setValue(Value value) {

return this.value = value;

}

}

/**

* Constructor.

* @param size the size of the pooled map;

*/

public CachePool(int size) {

maxCount = size;

}

@Override

public Value put(Key key, Value value) {

while(queue.size() >= maxCount){

queue.remove();

}

queue.add(new Entry(key, value));

return value;

}

@Override

public Value get(Object key){

for(Iterator iter = queue.iterator();iter.hasNext();){

Entry type = iter.next();

if(type.key.equals(key)){

queue.remove(type);

queue.add(type);

return type.value;

}

}

return null;

}

@Override

public Set> entrySet() {

Set> set = new HashSet>();

set.addAll(queue);

return set;

}

@Override

public void clear() {

queue.clear();

}

@Override

public Set keySet() {

Set set = new HashSet();

for(Entry e : queue){

set.add(e.getKey());

}

return set;

}

@Override

public Value remove(Object obj) {

for(Entry e : queue){

if(e.getKey().equals(obj)){

queue.remove(e);

return e.getValue();

}

}

return null;

}

@Override

public int size() {

return queue.size();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值