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

📄 blockingworkerqueue.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
字号:
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. The name "Exolab" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Exoffice Technologies.  For written permission,
 *    please contact info@exolab.org.
 *
 * 4. Products derived from this Software may not be called "Exolab"
 *    nor may "Exolab" appear in their names without prior written
 *    permission of Exoffice Technologies. Exolab is a registered
 *    trademark of Exoffice Technologies.
 *
 * 5. Due credit should be given to the Exolab Project
 *    (http://www.exolab.org/).
 *
 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2000 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: BlockingWorkerQueue.java,v 1.4 2003/08/07 13:33:12 tanderson Exp $
 *
 * Date         Author  Changes
 * 5/15/2001    jima    Created
 */


package org.exolab.jms.util;

import java.util.LinkedList;


/**
 * A BlockingWorkerQueue is attached to a QueueWorker. Any thread can add
 * work to the queue. The BlockingWorkerQueue, which extends Thread, blocks
 * waiting for work to arrive. When work arrives it will be dequeued and
 * passed down to the QueueWorker for processing.
 * <p>
 * The owner can use <code>close</code> to gracefully shutdown the thread.
 *
 * @version     $Revision: 1.4 $ $Date: 2003/08/07 13:33:12 $
 * @author      <a href="mailto:jima@exolab.org">Jim Alateras</a>
 */
public class BlockingWorkerQueue
    extends Thread {

    /**
     * This is the runnable that will perform the work
     */
    private QueueWorker _worker = null;

    /**
     * The list is used to queue work
     */
    private LinkedList _queue = new LinkedList();

    /**
     * If this is set to true then we must close the worker queue
     */
    private boolean _closed = false;


    /**
     * Construct a blocking worker queue and attahc to the specified
     * queue worker. Make the thread a deamon thread.
     *
     * @param name - the name of the thread
     * @param worker - the queue worker attached to this queue
     */
    public BlockingWorkerQueue(String name, QueueWorker worker) {
        super("BWQ:" + name);
        _worker = worker;
        this.setDaemon(true);
    }

    /**
     * Add some work to the end of the queue
     *
     * @param work - add this piece of work
     */
    public void add(Object object) {
        synchronized (_queue) {
            _queue.addLast(object);
            _queue.notifyAll();
        }
    }

    /**
     * This method is called once the thread has started. It basically loops
     * forever removing work from the queue and passing it to the runnable.
     * <p>
     * Calling <code>close</code> on this object will terminate it.
     */
    public void run() {
        while (!_closed) {
            Object work = null;

            synchronized (_queue) {
                // if there are no messages in the queue then block
                if (_queue.size() == 0) {
                    try {
                        _queue.wait();
                    } catch (InterruptedException exception) {
                        // ignore the exception;
                    }
                    continue;
                }

                // if we get this far then there is something in the queue.
                // Remove it and hand it to the worker
                work = _queue.removeFirst();
            }

            if (work != null) {
                _worker.execute(work);
            }
        }
    }

    /**
     * Return the number of entries still on the queue
     *
     * @size int
     */
    public int size() {
        return _queue.size();
    }

    /**
     * Close this worker queue
     */
    public void close() {
        _closed = true;
        interrupt();
    }

}

⌨️ 快捷键说明

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