public class Tmp extends Thread { // private ExecutorService pool = null; private static ThreadPoolExecutor pool = null; private static final int CORE_POOL_SIZE = 1; private static final int MAX_POOL_SIZE = 3; private static final long KEEPALIVE_TIME = 60; private static final TimeUnit TIME_UNIT = TimeUnit.SECONDS; private static final int QUEUE_CAPACITY = (CORE_POOL_SIZE + MAX_POOL_SIZE) / 2; private static BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>( QUEUE_CAPACITY); private static RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.DiscardOldestPolicy();
static { System.out.println("程序启动中... ..."); System.out.print("程序初始化... ..."); pool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEPALIVE_TIME, TIME_UNIT, workQueue, rejectedExecutionHandler);
package test;
import java.io.Serializable;
public class Process implements Serializable, Runnable {
/**
*
*/
private static final long serialVersionUID = 1;
private String xml="";
public Process(String xml) {
this.xml=xml;
}
public void run() {
try {
System.out.println(xml);
Thread.sleep(5000);
throw new Exception();
}catch(Exception e) {
e.printStackTrace();
}
}
}
*************************************package test;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Tmp extends Thread {
// private ExecutorService pool = null;
private static ThreadPoolExecutor pool = null;
private static final int CORE_POOL_SIZE = 1;
private static final int MAX_POOL_SIZE = 3;
private static final long KEEPALIVE_TIME = 60;
private static final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
private static final int QUEUE_CAPACITY = (CORE_POOL_SIZE + MAX_POOL_SIZE) / 2;
private static BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(
QUEUE_CAPACITY);
private static RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.DiscardOldestPolicy();
static {
System.out.println("程序启动中... ...");
System.out.print("程序初始化... ...");
pool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE,
KEEPALIVE_TIME, TIME_UNIT, workQueue, rejectedExecutionHandler);
System.out.println(" [ok]");
}
public void start() {
pool.execute(new Process("1111111111"));
pool.execute(new Process("2222222222"));
pool.execute(new Process("3333333333"));
pool.execute(new Process("4444444444"));
pool.execute(new Process("5555555555"));
pool.execute(new Process("6666666666"));
pool.execute(new Process("7777777777"));
pool.execute(new Process("88888888888"));
System.out.println("zzzzzzzz");
}
public static void main(String args[]) {
new Tmp().start();
}
}