📄 crbtqueue.java
字号:
package com.wireless.crbt.gwif.global.pub;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;
import org.apache.log4j.Logger;
public class CRBTQueue {
/*** The Log to which logging calls will be made. */
private static Logger log = Logger.getLogger(CRBTQueue.class);
static private CRBTQueue _instance;
private LinkedList list = new LinkedList();
// private long defaultTimeout = 10000;
public CRBTQueue() {
}
/** 线程同步控制确保模块仅有一个实例 */
static synchronized public CRBTQueue getInstance() {
if (_instance == null) {
_instance = new CRBTQueue();
}
return _instance;
}
/***
* Returns the current number of object in the queue
*/
public synchronized int size() {
return list.size();
}
/***
* adds a new object to the end of the queue.
* At least one thread will be notified.
*/
public synchronized void add(Object object) {
list.add(object);
notify();
}
/***
* Removes the first object from the queue, blocking until one is available.
* Note that this method will never return null and could block forever.
*/
public synchronized Object remove() {
while (true) {
Object answer = removeNoWait();
if (answer != null) {
return answer;
}
try {
wait();
} catch (InterruptedException e) {
log.error("Thread was interrupted: " + e, e);
}
}
}
/***
* Removes the first object from the queue, blocking only up to the given
* timeout time.
*/
public synchronized Object remove(long timeout) {
Object answer = removeNoWait();
if (answer == null) {
try {
wait(timeout);
} catch (InterruptedException e) {
log.error("Thread was interrupted: " + e, e);
}
answer = removeNoWait();
}
return answer;
}
/***
* Removes the first object from the queue without blocking.
* This method will return immediately with an item from the queue or null.
*
* @return the first object removed from the queue or null if the
* queue is empty
*/
public synchronized Object removeNoWait() {
if (!list.isEmpty()) {
return list.removeFirst();
}
return null;
}
/**
* ***************** 为增加优先级而增加的属性及运算方法 *************************
*/
private Map map = new HashMap();
private int circle = 1;
/**
* @param map HashMap
* 结构 map.put("1", "50");
* map.put("2", "30");
* .
* .
* .
* 最高优先级 1
*/
public CRBTQueue(HashMap map){
this.map = map;
for(int i=1; i<=map.size(); i++) {
String value = (String)map.get(i+"");//转化成字符串类型int+""
CRBTQueue queue = new CRBTQueue();
PriorityQueue priorityQueue = new PriorityQueue();
priorityQueue.setRight( Integer.parseInt(value) );
priorityQueue.setQueue( queue );
priorityQueue.setCounter(0);
map.put(i+"", priorityQueue);
}
}
/***
* adds a new object to the end of the queue by priority.
* At least one thread will be notified.
*/
public synchronized void add(String level, Object object) {
if( map.containsKey(level) ){
((CRBTQueue)(((PriorityQueue)map.get(level)).getQueue())).add(object);
}
else{
((CRBTQueue)(((PriorityQueue)map.get(map.size()+"")).getQueue())).add(object);
}
}
/***
* Returns the current number of object in all queue
*/
public synchronized int sizeByPriority() {
int tempCounter = 0;
for(int i=1; i<=map.size(); i++){
tempCounter += ((CRBTQueue)(((PriorityQueue)map.get(i+"")).getQueue())).size();
}
return tempCounter;
}
/**
* Returns all queue in the map
* @return Vector
*/
public synchronized Vector getPriorityQueue(){
Vector result = new Vector();
for(int i=1; i<=map.size(); i++){
result.add( ((CRBTQueue)(((PriorityQueue)map.get(i+"")).getQueue())) );
}
return result;
}
/***
* 去对象的算法如下:
* 1、实现各个队列的轮循
* 2、根据各个队列的权值从队列里取对象(直到累计值达到权值或者队列里为空)
*/
public synchronized Object removeByPriority() {
Object obj = null;
int counter;
int right ;
CRBTQueue queue = null;
for( int j=0; j<=map.size() && obj == null; j++){
counter = ( (PriorityQueue) map.get(circle + "")).getCounter();
right = ( (PriorityQueue) map.get(circle + "")).getRight();
if (counter < right) {
queue = (CRBTQueue) ( (PriorityQueue) map.get(circle + "")).getQueue();
if (queue.size() > 0) {
( (PriorityQueue) map.get(circle + "")).setCounter(counter + 1);
obj = queue.removeNoWait();
}
else{
circle = (circle % map.size()) + 1;
}
}
else {
((PriorityQueue)map.get(circle + "")).setCounter(0);
circle = (circle % map.size()) + 1;
}
}
return obj;
}
class PriorityQueue {
private int right;
private Object queue;
private int counter;
public int getRight() {
return right;
}
public void setRight(int right) {
this.right = right;
}
public Object getQueue() {
return queue;
}
public void setQueue(Object queue) {
this.queue = queue;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
}
public static void main(String args[]){
HashMap map = new HashMap();
map.put("1", "40");
map.put("2", "30");
map.put("3", "20");
map.put("4", "10");
CRBTQueue mtQueue = new CRBTQueue(map);
for (int j = 1; j <= map.size(); j++)
for (int i = 1; i <= 50; i++){
mtQueue.add(j + "", "[" + j + "][" + i + "]");
}
for(int i=1; i<=200; i++){
System.out.println(mtQueue.removeByPriority());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -