public synchronized void start(Runnable target, int priority) ... {
Pooled thread = null ;
List idleList = this ._idxThreads[priority];
int idleSize = idleList.size();
if (idleSize > 0 ) ... {
int lastIndex = idleSize - 1 ;
thread = (Pooled)idleList.get(lastIndex);
idleList.remove(idleList);
thread.setTarget(target);
} else ... {
this ._threadCount ++ ;
thread = new Pooled(target, " Pooled-> " + this ._threadCount, this );
switch (priority) ... {
case PRIORITY_LOW:
thread.setPriority(Thread.MIN_PRIORITY);
break ;
case PRIORITY_NORMAL:
thread.setPriority(Thread.NORM_PRIORITY);
break ;
case PRIORITY_HIGH:
thread.setPriority(Thread.MAX_PRIORITY);
break ;
default :
thread.setPriority(Thread.NORM_PRIORITY);
}
// 启动
thread.start();
}
}
/** */ /**
* 返回线程数量
*
* @return
*/
public int getThreadsCount() ... {
return this ._threadCount;
}
}
Pooled.java:
package org.loon.framework.util.test;
/** */ /**
*
* Title: LoonFramework
*
*
* Description:
*
*
* Copyright: Copyright (c) 2007
*
*
* Company: LoonFramework
*
*
*/
public class Pooled extends Thread ... {
private ThreadPool _pool;
private Runnable _target;
private boolean _shutdown = false ;
private boolean _idle = false ;
public Pooled(Runnable target) ... {
super (target);
}
public Pooled(Runnable target, String name) ... {
super (target, name);
}
public Pooled(Runnable target, String name, ThreadPool pool) ... {
super (name);
this ._pool = pool;
this ._target = target;
}
public Pooled(String name) ... {
super (name);
}
public Pooled(ThreadGroup group, Runnable target) ... {
super (group, target);
}
public Pooled(ThreadGroup group, Runnable target, String name) ... {
super (group, target, name);
}
public Pooled(ThreadGroup group, String name) ... {
super (group, name);
}
public Runnable getTarget() ... {
return this ._target;
}
public boolean isIdle() ... {
return this ._idle;
}
public void run() ... {
while ( ! this ._shutdown) ... {
this ._idle = false ;
if ( this ._target != null ) ... {
this ._target.run();
}
this ._idle = true ;
try ... {
this ._pool.repool( this );
synchronized ( this ) ... {
wait();
}
} catch (InterruptedException ex) ... {
System.err.println(ex.getMessage());
}
this ._idle = false ;
}
}
public synchronized void setTarget(Runnable target) ... {
this ._target = target;
notifyAll();
}
public synchronized void shutDown() ... {
this ._shutdown = true ;
notifyAll();
}
}
测试用类:
package org.loon.framework.util.test;
/** */ /**
*
Title: LoonFramework
*
Description:线程池测试
*
Copyright: Copyright (c) 2007
*
Company: LoonFramework
*/
public class ThreadPoolTest ... {
private static Runnable createRunnable( final int id) ... {
return new Runnable() ... {
public void run() ... {
System.out.println( " 线程 " + id + " ,运行 " );
try ... {
Thread.sleep( 1000 );
}
catch (InterruptedException ex) ... { }
System.out.println( " 线程 " + id + " ,结束 " );
}
} ;
}
public static void main(String[]args) ... {
ThreadPool pool = ThreadPool.getInstance();
pool.setDebug( true );
for ( int i = 1 ; i <= 10 ; i ++ ) ... {
// 根据数值,设定不同优先级
if (i % 2 == 0 ) ... {
pool.start(createRunnable(i), ThreadPool.PRIORITY_HIGH);
} else ... {
pool.start(createRunnable(i), ThreadPool.PRIORITY_LOW);
}
}
System.out.println( " 线程池测试中…… " );
System.out.println( " 线程池线程总数: " + pool.getThreadsCount());
pool.shutDown();
}
}
责任编辑:小草