import java.util.Vector; /*** * 双缓冲消息队列 * @author Donald * */ public class MessageQueueThread extends Thread { public int capacity=50; public boolean autoIncrement=false; private Vector readQueue=new Vector(capacity); private Vector writeQueue=new Vector(capacity); // private Object readLock=new Object(); private Object writeLock=new Object(); private MessageHandler handler; public void run(){ if(handler==null) return; Message msg=null; while (true) { if (!readQueue.isEmpty()) { msg=(Message) readQueue.firstElement(); readQueue.removeElement(msg); if(msg!=null) handler.onReceive(msg); msg=null; } else { synchronized (writeLock) { if (!writeQueue.isEmpty()) { readQueue =writeQueue; writeQueue=null; writeQueue=new Vector(capacity); } } System.gc(); } try { Thread.sleep(10); } catch (InterruptedException e) { } } } public void sendMessage(Message msg){ synchronized (writeLock) { if(writeQueue.size()