listener.java~1~

来自「封装了SQL、Socket、WAP、MIME等功能的通用组件」· JAVA~1~ 代码 · 共 83 行

JAVA~1~
83
字号
/**
 * <p>Title: 即时通信</p>
 * <p>Description: 即时通信服务器</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 深圳市科皓信息技术有限公司</p>
 * @author 刘学
 * @version 1.0
 */
package com.lazybug.util;

import java.util.ArrayList;

public abstract class Listener implements Runnable
{
    protected Thread thread;
    protected boolean isRunning;
    protected boolean isBusy = true;      //是否忙
    protected long calculagraph;      //计时器
    protected String remark;
    private static ArrayList threadQuene = new ArrayList();
    private static int thread_count;
    public Listener()
    {
        this.isRunning = false;
    }

    public static void printThreadStatus()
    {
        for(int i = 0; i < threadQuene.size(); i++ )
        {
            String str = ((Listener)threadQuene.get(i)).status();
            if( str != null ) System.out.println(str);
        }
    }

    public void start()
    {
        thread = new Thread(this);
        this.isRunning = true;
        thread.start();
        synchronized( threadQuene )
        {
            threadQuene.add( this );
        }
    }

    /**
     * 强制性中断
     */
    public void halt()
    {
        if( this.thread != null && this.isRunning)
        {
            Thread remover = this.thread;
            remover.interrupt();
            this.thread = null;
            this.isRunning = false;
            synchronized( threadQuene )
            {
                threadQuene.remove( this );
            }
        }
    }

    /**
     * 发出停止的指令
     */
    public void stop()
    {
        this.isRunning = false;
        synchronized( threadQuene )
        {
            threadQuene.remove( this );
        }
    }

    public String status()
    {
        if( remark == null ) return null;
        return remark+":"+(isRunning?(this.isBusy?"忙["+this.calculagraph+"]":"空闲"):"线程已关闭");
    }

}

⌨️ 快捷键说明

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