您的位置:首页 > 其它

通过Socket实现进程间异步通讯(一)

2004-09-08 17:03 549 查看
第一步写一个线程类 CommThread.java
package com.hode.thread;

/**
 * @author 泰伯子仪 
  */
public abstract class CommThread extends Thread
{
   
    private boolean shouldRun = false;

    private int sleepTime = 2000;

    private boolean isWait = false;

    private boolean isWork = true;
   
    /**
     * 
     */
    public CommThread()
    {
        //super("CommThread");
        Thread  commThread = new Thread(CommThread.this);
        shouldRun = true;
    }
   
    public CommThread(int sleepTime)
    {
        //super("CommThread");
        Thread  commThread = new Thread(CommThread.this);
        shouldRun = true;
        this.sleepTime = sleepTime;
    }
   
    public void threadStart()
    {
        start();
    }

    public void run()
    {
        while (shouldRun)
        {
            try
            {
                sleep(sleepTime);
            }
            catch (InterruptedException e)
            {
                System.err.println("线程Thread1意外终止。");
            }
            if (!getWait())
            {
                isWork = true;
                System.out.println("/n处理开始");
               
                dispose();

                isWork = false;
                System.out.println("/n处理完毕");
            }
            else
            {
                isWork = false;
                System.out.println("等待中.../n");
            }
        }
    }
   
    public void shutdown()
    {
        shouldRun = false;
    }

    public void setWait(boolean bool)
    {
        isWait = bool;
    }

    public boolean getWait()
    {
        return isWait;
    }

    public boolean getWork()
    {
        return isWork;
    }
   
    public abstract void dispose();
   
}

该类为虚类,其中需函数public abstract void dispose()是线程中需要处理的程序部分
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  socket 通讯 thread class
相关文章推荐