public class ReusableThread implements Runnable {
//线程状态监听者接口
public interface ThreadStateListener {
public abstract void onRunOver(ReusableThread thread);//当用户过程执行完毕后调用的方法
}
public static final byte STATE_READY=0; //线程已准备好,等待开始用户过程
public static final byte STATE_STARTED=1; //用户过程已启动
public static final byte STATE_DESTROYED=2; //线程最终销毁
byte mState; //标示可重启线程的当前状态
Thread mThread; //实际的主线程对象
Runnable mProc; //用户过程的run()方法定义在mProc中
ThreadStateListener mListener; //状态监听者,可以为null
/** Creates a new instance of ReusableThread */
public ReusableThread(Runnable proc) {
mProc = proc;
mListener = null;
mThread = new Thread(this);
mState = STATE_READY;
}
public byte getState() {return mState;}
public void setStateListener(ThreadStateListener listener) {
mListener = listener;
}
/**可以在处于等待状态时调用该方法重设用户过程*/
public synchronized boolean setProcedure(Runnable proc) {
if (mState == STATE_READY) {
mProc = proc;
return true;
}
else
return false;
}
/**开始执行用户过程*/
public synchronized boolean start() {
if (mState == STATE_READY) {
mState = STATE_STARTED;
if (!mThread.isAlive()) mThread.start();
notify(); //唤醒因用户过程执行结束而进入等待中的主线程
return true;
}
else
return false;
}
/**结束整个线程,销毁主线程对象。之后将不可再次启动*/
public synchronized void destroy() {
mState = STATE_DESTROYED;
notify();
mThread = null;
}
public void run() {
while (true) {
synchronized (this) {
try {
while (mState != STATE_STARTED) {
if (mState == STATE_DESTROYED) return;
wait();
}
} catch(Exception e) {e.printStackTrace();}
}
if (mProc != null) mProc.run();
if (mListener != null) mListener.onRunOver(this); //当用户过程结束后,执行监听者的onRunOver方法
synchronized (this) {
if (mState == STATE_DESTROYED) return;
mState = STATE_READY;
}
}
}
}
public class ThreadPool extends ObjectPool implements ReusableThread.ThreadStateListener {
public static final int DefaultNumThreads = 16; //默认池容量
public ReusableThread getThread() {
return (ReusableThread)fetch();
}
public void onRunOver(ReusableThread thread) {
recycle(thread); //当用户过程结束时,回收线程
}
private void init(int size) {
ReusableThread thread;
//初始化线程池内容
for (int i=0; i<size; i++) {
thread = new ReusableThread(null);
thread.setStateListener(this);
setElementAt(thread, i);
}
}
public ThreadPool(int size) {
super(size);
init(size);
}
public ThreadPool() {
super(DefaultNumThreads);
init(DefaultNumThreads);
}
}
/**字符放射器*/
class CharEmitter implements Runnable, ReusableThread.ThreadStateListener {
char c; //被发射的字符
boolean[] isEmitting; //标示某字符是否正被发射(直接以字符对应的ASCII码作下标索引)
ReusableThread thread; //可重启线程对象
ObjectPool myHomePool; //为知道应把自己回收到哪里,需要保存一个到自己所在对象池的引用
CharEmitter(ObjectPool container, boolean[] isCharEmitting) {
isEmitting=isCharEmitting;
myHomePool=container;
thread=new ReusableThread(this); //新建可重启线程对象,设其用户过程为CharEmitter类自己定义的
}
/**开始“发射”字符*/
public void emit(char ch) {
//字符被要求只能是'0'到'9'之间的数字字符
if (ch>='0' && ch<='9') {
c=ch;
}
else c=' ';
thread.start(); //启动线程
}
public void run() {
if (c==' ') return; //若不是数字字符直接结束
//为便于观察,不同数字之前的空格数目不同,以便将其排在不同列上
int spaceLen=c-'0';
StringBuffer s=new StringBuffer(spaceLen+1);
for (int i=0; i<spaceLen; i++) s.append(' ');
s.append(c);
while (isEmitting[c]) {
System.out.println(s); //不断地向屏幕写字符
}
}
/**实现线程状态监听者接口中的方法*/
public void onRunOver(ReusableThread t) {
myHomePool.recycle(this); //回收自身入池
}
}
public class TestThreadPool {
public static void main(String[] args) {
// TODO Auto-generated method stub
//标示字符是否正被发射的标志变量数组
boolean[] isEmitting=new boolean[256];
for (int i=0; i<256; i++) isEmitting[i]=false;
ObjectPool emitters=new ObjectPool(10); //新建对象池,容量为10
for (int i=0; i<10; i++) {
//用CharEmitter对象填满池子
emitters.setElementAt(new CharEmitter(emitters, isEmitting), i);
}
byte[] c=new byte[1];
CharEmitter emitter;
while(true) {
try {
System.in.read(c); //从键盘读入一个字符,以回车键表示输入结束
} catch(Exception e) {e.printStackTrace();}
if (isEmitting[c[0]]) {
isEmitting[c[0]]=false; //若字符正被发射,则结束其发射
}
else {
isEmitting[c[0]]=true;
emitter=(CharEmitter)emitters.fetch(); //向池中索取一个CharEmitter对象
emitter.emit((char)c[0]); //发射用户输入的字符
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有