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

📄 queue.java

📁 自己写的一个聊天的小程序 请多多指教
💻 JAVA
字号:
/* * Copyright (c) 2003 - 2007, Silvio Meier and Tobias Reinhard * * All rights reserved. *  * Redistribution and use in source and binary forms,  * with or without modification, are permitted provided  * that the following conditions are met: * *   o Redistributions of source code must retain the above  *     copyright notice, this list of conditions and the  *     following disclaimer. *   o 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. *   o The names of its contributors may not be used to endorse  *     or promote products derived from this software without  *     specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS 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 THE COPYRIGHT OWNER OR * 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. */package net.sf.cscc;import java.util.Vector;/** *  This class is a thread safe event queue, ie. data events are stored *  in the order they occurred (FIFO). * *	@author             Silvio Meier (SM) *	@copyright          Silvio Meier, Tobias Reinhard, 2003 *	@history            2003-05-09 SM First Version *                      2003-05-14 SM working on additional methods and events, *                      correction of some comments. *                      2003-05-20 TR Comments corrected *                      2003-06-13 SM Addtional method for querying size of the queue. * 						2004-12-13 SM Some cleaning of unused things. * 						2006-11-28 SM Revised comments. *	@version            $Date: 2007/07/01 17:04:05 $, $Author: reode_orm $, $Revision: 1.1 $ */class Queue {        /**     *	Used to store all the data events which recently occurred.     */    private Vector dataEvents;        /**        * Creates the thread safe event queue and initializes the container     * i.e. the Vector object in which the events are stored.     *      * @pre true     * @post dataEvents != null     */    public Queue() {        dataEvents = new Vector();    }        /**     *   Enqueues a new DataEvent object at the end of the event queue.     *        *   @pre de != null     *   @post (dataEvents.size())@pre == (dataEvents.size())@post + 1     *   @param de DataEvent object which is enqued at the end of the queue.     */    public synchronized void enqueueDataEvent(DataEvent de) {        dataEvents.add(de);    }        /**     *   Returns the oldest event in the event queue and deletes it from     *   the queue.     *        *   @pre dataEvents.size() > 0     *   @post (dataEvents.size())@pre == (dataEvents.size())@post - 1     *   @return Returns the oldest DataEvent Element in the queue.     */    public synchronized  DataEvent dequeueDataEvent() {        Object ret = dataEvents.elementAt(0);        dataEvents.remove(0);        return (DataEvent)ret;    }        /**     *   This method returns the oldest element from the queue without removing     *   it from the queue.     *        *   @pre  dataEvents.size() > 0     *   @post true     *   @invariant (dataEvents.size())@pre == (dataEvents.size())@post     *   @return Oldest DataEvent object from the queue.     */    public synchronized  DataEvent getDataEvent() {        Object ret = dataEvents.elementAt(0);        return (DataEvent) ret;    }            /**     *   Removes the oldest DataEvent object from the queue.     *        *   @pre dataEvents.size() > 0     *   @post (dataEvents.size())@pre == (dataEvents.size())@post - 1     */    public synchronized  void removeDataEvent() {        dataEvents.remove(0);    }        /**     *   This method returns true, if there are DataEvents in the queue.     *   This method can be used, if clients using the communication component     *   implement a pull model instead of a push (observer) model.     *        *   @pre true     *   @post true     *   @invariant(dataEvents.size())@pre == (dataEvents.size())@post     *        *   @return Returns true, if there is at least one element in the     *           queue.     */    public synchronized boolean hasDataEvents() {        return (dataEvents.size() > 0);    }        /**     *   This method returns the size of the queue.     *        *   @pre true     *   @post true     *        *   @return Returns the number of elements in the queue. If no elements     *           are in the queue, nothing is returned.     */    public synchronized int size() {        return dataEvents.size();    }}

⌨️ 快捷键说明

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