java線程池處理並發業務

來源:互聯網
上載者:User

標籤:null   不為   緩衝   私人化   blocking   java線程池   epo   nal   ash   

/**
* 緩衝對象 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();
}
}

 

java線程池處理並發業務

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.