`

java queue

 
阅读更多

package com.ruyicai.queue;



import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.locks.ReentrantLock;
import com.ruyicai.generalinterface.*;
import com.ruyicai.main.MobilePlatLog;


/**
 *队列管理类,提供队列操作的方法
 * @author root
 *
 */
public class QueueManager {

	private static BlockingDeque<Deliverpackage> deliverqueue=new LinkedBlockingDeque<Deliverpackage>();
	private static BlockingDeque<Submitpackage> submitqueue=new LinkedBlockingDeque<Submitpackage>();
	private static ReentrantLock delivertakelock=new ReentrantLock();
	private static ReentrantLock deliverputlock=new ReentrantLock();
	private static ReentrantLock submittakelock=new ReentrantLock();
	private static ReentrantLock submitputlock=new ReentrantLock();



	/**
	 * 从上行队列中取一条消息
	 * @param 
	 * @return 上行消息对象
	 * @throws InterruptedException 
	 */
	public static Deliverpackage getDeliverPackage() throws InterruptedException{
		try{
			delivertakelock.lock();
			return (Deliverpackage)deliverqueue.takeLast();
		}
		finally{
			delivertakelock.unlock();
		}
	}
	
	/**
	 * 往上行队列中写一条消息
	 * @param 上行消息对象
	 * @return 
	 * @throws InterruptedException 
	 */
	public static void putDeliverPackage(Deliverpackage deliverpkg) throws InterruptedException{
		try{
			deliverputlock.lock();
			deliverqueue.putFirst(deliverpkg);
			MobilePlatLog.log("当前上行队列数据:"+deliverqueue.size()+"条");
		}
		finally{
			deliverputlock.unlock();
		}
	}
	
	/**
	 * 获取上行队列当前消息数量
	 * @param 
	 * @return 上行队列当前消息数量
	 */	
	public static int GetDeliverQueueSize(){
		return deliverqueue.size();
	}
	
	/**
	 * 从下行队列中取一条消息
	 * @param 
	 * @return 下行消息对象
	 * @throws InterruptedException 
	 */
	public static Submitpackage getSubmitPackage() throws InterruptedException{
		try{
			submittakelock.lock();
			return (Submitpackage)submitqueue.takeLast();
		}
		finally{
			submittakelock.unlock();
		}
	
	}
	
	/**
	 * 往下行队列中写一条消息
	 * @param 下行消息对象
	 * @return 
	 * @throws InterruptedException 
	 */	
	public static void putSubmitPackage(Submitpackage submitpkg) throws InterruptedException{
		try{
			submitputlock.lock();
			submitqueue.putFirst(submitpkg);
			MobilePlatLog.log("当前下行队列数据:"+submitqueue.size()+"条");
		}
		finally{
			submitputlock.unlock();
		}
	}
	 
	/**
	 * 获取下行队列当前消息数量
	 * @param 
	 * @return 下行队列当前消息数量
	 */	
	public static int getSubmitQueueSize(){
		return submitqueue.size();
	
	}
	
	/**
	 * 获取上行队列
	 * @param 
	 * @return 上行队列对象
	 */	
	public BlockingDeque<Deliverpackage> getDeliverQueue(){
		return this.deliverqueue;
	}
	 
	/**
	 * 获取下行队列
	 * @param 
	 * @return 下行队列对象
	 */	
	public BlockingDeque<Submitpackage> getSubmitQueue(){
		return this.submitqueue;
	
	}

}



更多内容http://www.ibm.com/developerworks/cn/java/j-5things4.html?ca=drs-
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics