日期:2014-05-20 浏览次数:20701 次
package a.b.threadpool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TpTest {
public static void main(String[] args){
BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<Runnable>(20);
ThreadPoolExecutor pool = new ThreadPoolExecutor(3,5,20,TimeUnit.MILLISECONDS,bqueue);
pool.execute(new XianChen("线程1"));
pool.execute(new XianChen("线程2"));
pool.execute(new XianChen("线程3"));
pool.execute(new XianChen("线程4"));
pool.execute(new XianChen("线程5"));
pool.execute(new XianChen("线程6"));
pool.execute(new XianChen("线程7"));
pool.execute(new XianChen("线程8"));
pool.execute(new XianChen("线程9"));
pool.execute(new XianChen("线程10"));
pool.execute(new XianChen("线程11"));
pool.shutdown();
}
}
class XianChen extends Thread{
String name;
XianChen(String name){
this.name = name;
}
public void run() {
System.out.println(Thread.currentThread().getName()+"进入");
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(Thread.currentThread().getName()+"退出");
}
}
public ThreadPoolExecutor(int corePoolSize,在这构造方法中,优先级处理任务的优先级为:
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)