public static void main(String[] args) throws Exception { // 构造一个自定义参数的线程池 //ThreadPoolExecutor threadPool = new ThreadPoolExecutor(20, 40, 30, TimeUnit.SECONDS, new ArrayBlockingQueue(3), new ThreadPoolExecutor.DiscardOldestPolicy()); //构造一个默认方式的线程池 ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); for(int i=1; i<=10; i++) { // 产生一个任务,并将其加入到线程池 String task = "task@ " + i; threadPool.execute(new ThreadPoolTask(task)); } } //线程所执行的任务 public static class ThreadPoolTask implements Runnable { private Object threadPoolTaskData; ThreadPoolTask(Object tasks){ this.threadPoolTaskData = tasks; } public void run() { System.out.println("start .." + threadPoolTaskData); threadPoolTaskData = null; } } }