⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 respqueue.java

📁 中国联通炫铃业务接口开发
💻 JAVA
字号:
package com.wireless.crbt.gwif.global.pub;

import java.util.LinkedList;

import org.apache.log4j.Logger;


public class RespQueue {
	/*** The Log to which logging calls will be made. */
    private static Logger log = Logger.getLogger(RespQueue.class);
    static private RespQueue _instance;
    private LinkedList list = new LinkedList();
//    private long defaultTimeout = 10000;

    public RespQueue() {
    }

    /** 线程同步控制确保模块仅有一个实例 */
    static synchronized public RespQueue getInstance() {
        if (_instance == null) {
            _instance = new RespQueue();
        }
        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) {
        //log.debug("MOQueue add Object"+object.toString());
        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() {
        //log.debug("remove this MOQueue Object");
        if (!list.isEmpty()) {
            return list.removeFirst();
        }
        return null;
    }

}

⌨️ 快捷键说明

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