objectqueue.java

来自「短信开发用于文件交换处理转发的类模块」· Java 代码 · 共 120 行

JAVA
120
字号
package com.pub.dataframe;

import java.util.HashMap;
import java.util.LinkedList;

import org.apache.log4j.Logger;

public class ObjectQueue {

	/** * The Log to which logging calls will be made. */
	private static Logger log = Logger.getLogger(ObjectQueue.class);

	private LinkedList <Object> list = new LinkedList<Object>();

	private static HashMap<String, ObjectQueue> instances = new HashMap<String, ObjectQueue>(10);

	private String queueName = "";

	/**
	 * 私有的构造子保证外界无法直接将此类实例化
	 */
	private ObjectQueue() {
		// do nothing
	}

	/**
	 * 私有的构造子保证外界无法直接将此类实例化
	 */
	private ObjectQueue(String queueName) {
		this.queueName = queueName;
	}

	/**
	 * 工厂方法,返还一个具有指定的内部状态的实例
	 */
	public synchronized static ObjectQueue getInstance(String queueName) {
		if (instances.containsKey(queueName)) {
			return instances.get(queueName);
		} else {
			ObjectQueue temp = new ObjectQueue(queueName);
			instances.put(temp.getQueueName(), temp);
			return temp;
		}
	}

	public String getQueueName() {
		return queueName;
	}

	public void setQueueName(String queueName) {
		this.queueName = queueName;
	}

	/***************************************************************************
	 * 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();
			} 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;
	}

}

⌨️ 快捷键说明

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