最近写进程间通讯有所心得,故而在此记录以备后忘,也希望大家多提建议
第一步写一个线程类 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()是线程中需要处理的程序部分