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

📄 datadispatcher.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;/** *   The DataDispatcher class provides a single data dispatcher for each client *   or a server. This class dispatches all incoming *   {@link net.sf.cscc.DataEvent} objects to the interested observers. * *   @author            Silvio Meier (SM) *   @copyright      	Silvio Meier, Tobias Reinhard, 2003 *   @history			2003-06-04 SM First Version *                      2003-06-12 SM Some corrections in the comments. *                      2003-06-14 SM Problem with deadlock in some situation *                                    solved. *                      2006-11-02 SM Comments revised. *   @version			$Date: 2007/07/01 17:04:05 $, $Author: reode_orm $, $Revision: 1.1 $ */class DataDispatcher extends java.lang.Thread {    /**     * The data observer which is responsible for sending messages.     */    protected DataEventObservable dataObservable;    /**     *  Reference for the incoming message queue.     */    protected Queue inComingMessages;    /**     *  This flag sets, if the dispatcher thread is running, i.e. that     *  new data events are dispatched.     */    protected boolean dispatching = false;    /**     *   Intitializes the data event dispatcher and starts the     *   dispatching thread.     *   @pre deo != null && outgoing != null     *   @post dispatching == true     *   @param deo The observable mechanism which notifies all the     *              interested observers.     *   @param incoming Incoming queue containing all the     *              received data events.     */    public DataDispatcher(DataEventObservable deo, Queue incoming) {        this.dataObservable = deo;        this.inComingMessages = incoming;        startDispatching();    }    /**     *   Starts the dispatching thread.     *   @pre isDispatching() == false     *   @post isDispatching() == true     */    public void start() {        dispatching = true;        super.start();    }    /**     *   Starts the dispatching. See {@link #start()}.     *   @pre isDispatching() == false     *   @post isDiaptching() == true     */    protected void startDispatching() {        this.start();    }    /**     *   Stops the dispatching thread (ie. dispatchig mechanism).     *   @pre isDispatching() == true     *   @post isDispatching() == false     */    public void stopDispatching() {        dispatching = false;        // if the dispatcher thread is still blocked        // wake up thread again.        synchronized (inComingMessages) {            inComingMessages.notifyAll();        }    }    /**     *   This method is called to notify the dispatcher thread about     *   new enqueued DataEvent objects in the event queue.     *   @pre true     *   @post true     */    public void notifyDispatch() {        synchronized (inComingMessages) {            inComingMessages.notifyAll();        }    }    /**     *   This method implements the dispatching mechanism, ie. it calls     *   the observers for notifying for occurring data events.     *     *   @pre true     *   @post isDispatching() == false     */    public void run() {        boolean iterateMore;        while (dispatching) {            // flag for checking if all            // event receivers got the DataEvent object            iterateMore = true;            while (inComingMessages.hasDataEvents() && iterateMore) {                iterateMore = dataObservable.notifyDataEventObservers(inComingMessages.getDataEvent());                if (iterateMore) {                    inComingMessages.removeDataEvent();                }            }            try {                synchronized(inComingMessages) {                    inComingMessages.wait();                }            } catch (InterruptedException ex) {                // does nothing            }        }    }    /**     *  This method returns true if the dispatching mechanism is currently     *  active, ie. if it is waiting for incoming messages to dispatch. Otherwise,     *  false is returned.     *  @return Returns true, if the data dispatcher is active.     */    public boolean isDispatching() {        return dispatching;    }}

⌨️ 快捷键说明

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