📄 eventqueue.java
字号:
/*
* Created on 2003-12-8
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package com.product.util.event;
import java.util.Vector;
import com.product.util.CollectionUtil;
/**
* @author Renzhichao
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public final class EventQueue {
private Vector queue ;
class Semaphore {
private int count;
public Semaphore(int n) {
this.count = n;
}
public synchronized void acquire() {
while(count == 0) {
try {
wait();
} catch (InterruptedException e) {
//keep trying
}
}
count--;
}
public synchronized void release() {
count++;
notifyAll(); //alert a thread that's blocking on this semaphore
}
}
Semaphore sem = null ;//信号量!
private void loadQueue(){
Event[] evts = EventSerialManager.load() ;
queue = CollectionUtil.getInstance().convertArrarToVetor(evts) ;
if (null==queue){
queue = new Vector() ;
}
sem = new Semaphore(queue.size()) ;
}
private static EventQueue inst = new EventQueue() ;
private EventQueue(){
loadQueue() ;
}
public static EventQueue getInstance(){
return inst ;
}
/**
* 将一个Event放入到队列中来!
* 这个Event如果没有序列化,会自动进行序列化!
* @param evt
*/
public void putEvent(Event evt){
//首先判断这个Event是否是已经序列化的!
if (!evt.isSerialized()){
EventSerialManager.writeEvent(evt) ;
}
//用生产者模式来激活所有的消费者。
queue.add(evt) ;
sem.release() ;
}
/**
* 如果队列不为空,返回第一个Event。
* 如果队列为空,返回空。
* @return
*/
public synchronized Event getEvent(){
sem.acquire() ;//消费者线程获得信号量。
if (null!=queue&&queue.size()>0){
return (Event) queue.remove(0) ;
}
else {
return null ;
}
}
public synchronized Event getEvent(String className){
sem.acquire() ;
if (null!=queue&&queue.size()>0){
if (queue.get(0).getClass().getName().equals(className)){
return (Event)queue.remove(0) ;
}
else {
sem.release() ;
return null ;
}
}
else {
return null ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -