queue.java

来自「java 编写的短信平台。支持所有协议」· Java 代码 · 共 79 行

JAVA
79
字号
package com.sf.note;

import java.util.LinkedList;



public class Queue {
	LinkedList list = new LinkedList();
	
	private final int MaxQueueSize = 2000;

	private int discardMsg = 0;

	public Queue() {
	}

	@SuppressWarnings("unchecked")
	public synchronized void put(Object msg) throws InterruptedException {
		if (MaxQueueSize <= this.getQueueSize()) {
			discardMsg++;
			return;
		}
		if (msg == null) {
			return;
		}
		list.add(msg);
		this.notifyAll();
	}

	@SuppressWarnings("unchecked")
	public synchronized void put(Object msg, int flag)
			throws InterruptedException {
		if (MaxQueueSize <= this.getQueueSize()) {
			discardMsg++;
			return;
		}
		if (msg == null) {
			return;
		}
		list.add(msg);
		this.notifyAll();
	}

	@SuppressWarnings("unchecked")
	public synchronized void putFirst(Object msg) throws InterruptedException {
		if (MaxQueueSize <= this.getQueueSize()) {
			return;
		}
		if (msg == null) {
			return;
		}
		list.addFirst(msg);
		this.notifyAll();
	}

	public synchronized Object get() throws InterruptedException {
		while (list.isEmpty()) {
			try {
				this.wait();
			} catch (InterruptedException e) {
			}
		}
		return list.removeFirst();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public int getQueueSize() {
		return list.size();
	}

	public int getDiscardMsg() {
		return discardMsg;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?