非常好用屡试不爽的jdk自带线程池:设置线程池定义的线程个数
1 定义线程池
package mcparallelprocess.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 线程池构造
*
* @author Administrator
*/
public class ThreadPool {
private static ExecutorService pool;
/**
* 线程池线程数量,默认100
*/
private static int poolThreadCount = Integer.valueOf(System
.getProperty("poolThreadCount"));;
public static ExecutorService get() {
if (pool == null) {
pool = Executors.newFixedThreadPool(poolThreadCount);
}
return pool;
}
}
2 使用线程池
其中task为实现了Runnable接口的类
ThreadPool.get().execute(task);