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

📄 listenerthread.java

📁 用java实现的
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.qq;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.tsinghua.lumaqq.qq.events.*;
import edu.tsinghua.lumaqq.qq.packets.InPacket;


/**
 * 包事件监听线程,如果有包到达,则通知其他人
 *
 * @author 马若劼
 */
public class ListenerThread extends Thread {
    // Log对象
    protected static Log log = LogFactory.getLog(ListenerThread.class);
    // 端口类
    private IPort port;
    // 停止标志
    private volatile boolean stop;
    
    /**
     * @param receiver 接收类
     * @param lock 操作锁
     */
    public ListenerThread(IPort port) {
        this.port = port;
        this.stop = false;
        this.setName("Listener");
        this.setDaemon(true);
    }
    
    /**
     * 设置停止标志
     * @param stop true如果需要停止发送线程
     */
    public synchronized void setStop(boolean stop) {
        this.stop = stop;    		
        this.notify();
    }
    
    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        log.debug("包事件监听线程已经启动");
        InPacket packet = null;
        while(!isStop()) {
            // 等待,被唤醒后就得到下一个包
            synchronized(this) {
                try {
                    this.wait();
                } catch (InterruptedException e1) {
                }
	            if(stop) break;
            }
            packet = port.remove();
            while(packet != null) {
	            // 通知所有包事件监听器
	            PacketEvent e = new PacketEvent(packet);
	            port.firePacketArrivedEvent(e);
	            // 得到下一个包
	            packet = port.remove();
            }
        }
        log.debug("包事件监听线程正常退出");
    }
    
    /**
     * @return Returns the stop.
     */
    public synchronized boolean isStop() {
        return stop;
    }
}

⌨️ 快捷键说明

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