JAVAマルチスレッドとキュー

7069 ワード

住所はJAVAマルチスレッドとキューです。
         JAVAはすでに私達に比較的良い行列を提供してQueueを実現して、Collectionで継承します。今回はBlockingQueを使って、Queに引き継ぎます。   
         Conccurrentパッケージでは、Blocking Queは、マルチスレッドの中で、どのように効率的に安全な「転送」データを解決していますか?これらの高効率かつ安全なスレッドクラスを通じて、高速で高品質なマルチスレッドプログラムを構築することができます。
         まずBlockingQueを利用して、行列類を封入しました。列にMapオブジェクトを保存します。これはプロジェクトの要求によって決められています。参考にしてください。
         
import java.util.AbstractMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* map
*/

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();
}
}

            その中にはプロジェクトの需要に応じていくつかの方法が書き換えられました。
            まず消費者の種類を見て、マルチスレッドを使ってキューの内容を処理します。
      
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
* ,
*/
public class TicketTradeOper extends HttpServlet
{
/**
* map
*/
public static CachePool mapPool = CachePool.getInstance();

private static final int NTHREADS=5;
// 。
private static final Executor threadPool=Executors.newFixedThreadPool(NTHREADS);

//
IETicketTradeOper ticketTradeOper;

@Override
public void init() throws ServletException
{
Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
startThread();
}
}, new Date(), 5000);// 5
super.init();
}


public void startThread(){
threadPool.execute(new Runnable(){
public void run() {
executeCodeOper();
}
});
}

public void executeCodeOper()
{
String key = "";
Map param = null;
synchronized (mapPool)
{
System.out.println(Thread.currentThread().getName() + " 。。。。");
System.out.println(" ----"+mapPool.size()+"--- ");

Iterator it = mapPool.keySet().iterator();
// ,
while (it.hasNext())
{
key = (String) it.next();
param = (Map) mapPool.get(key);
}
if (null != param)
{
// ,
mapPool.remove(key);
}
}

if (null != param)
{
boolean result =ticketTradeOperator(param);
System.out.println(" ========"+result);
if(!result){
// ,
mapPool.put(key, param);
};
}
}


public boolean ticketTradeOperator(Map params)
{
//
return resultCode;
}

public IETicketTradeOper getTicketTradeOper()
{
return ticketTradeOper;
}
public void setTicketTradeOper(IETicketTradeOper ticketTradeOper)
{
this.ticketTradeOper = ticketTradeOper;
}

}
            生産者は、業務の需要に応じて受信したデータをキューに入れます。
     TicketTradeOper.mapPool.put(newParams.get("order_id"), newParams);
以上は全体の列の生産と消費の過程で、問題があるのは交流を歓迎します。キュー類Queueについて紹介します。次のブログで行います。
         
  
転載は本文の住所を明記してください。JAVAマルチスレッドとキュー