java线程池处理并发业务
2017-01-10 16:20
176 查看
/**
* 缓存对象 map
*/
public static CachePool<String, Object> 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<String,String>();
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<Key, Value> extends AbstractMap<Key, Value>{
// 私有化缓存对象实例
private static CachePool cachePool = new CachePool();
private int maxCount = 1000;
private BlockingQueue<Entry> queue = new LinkedBlockingQueue<Entry>();
/**
* 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<Key, Value>{
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<Entry> 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<Map.Entry<Key, Value>> entrySet() {
Set<Map.Entry<Key, Value>> set = new HashSet<Map.Entry<Key, Value>>();
set.addAll(queue);
return set;
}
@Override
public void clear() {
queue.clear();
}
@Override
public Set<Key> keySet() {
Set<Key> set = new HashSet<Key>();
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();
}
}
* 缓存对象 map
*/
public static CachePool<String, Object> 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<String,String>();
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<Key, Value> extends AbstractMap<Key, Value>{
// 私有化缓存对象实例
private static CachePool cachePool = new CachePool();
private int maxCount = 1000;
private BlockingQueue<Entry> queue = new LinkedBlockingQueue<Entry>();
/**
* 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<Key, Value>{
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<Entry> 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<Map.Entry<Key, Value>> entrySet() {
Set<Map.Entry<Key, Value>> set = new HashSet<Map.Entry<Key, Value>>();
set.addAll(queue);
return set;
}
@Override
public void clear() {
queue.clear();
}
@Override
public Set<Key> keySet() {
Set<Key> set = new HashSet<Key>();
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();
}
}
相关文章推荐
- 如何处理高并发业务场景
- 并发处理业务
- 并发请求导致的业务处理安全风险及解决方案
- 处理高并发的业务逻辑
- 编程经验点滴----使用接口表保存可能并发的业务,然后顺序处理
- 并发请求导致的业务处理安全风险及解决方案
- 注销类业务的处理
- 业务单号自动增长的处理办法
- 在WebService中利用SoapException处理业务流程执行的错误
- 操作NHibernate进行多事务并发处理的一些小经验
- 如何处理日志和业务逻辑的困惑
- JDK 5.0 Concurrency Utilities 并发处理(2)Condition 条件变量
- 处理数据并发
- 总结前段时间做的电话业务故障处理系统(1)
- 通过Message-Driven beans来添加并发处理
- spring处理业务示例(源码)
- 数据库乐观并发处理的策略
- Sql Server 并发处理
- JDK 5.0 Concurrency Utilities 并发处理(4)Semaphore 信号量
- JDK 5.0 Concurrency Utilities 并发处理(1)ReentrantLock 可重入锁