日期:2014-05-20 浏览次数:21304 次
package test; import java.io.IOException; import java.io.InputStream; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.Properties; public class TestThreadPool { private static int produceTaskSleepTime ; private static int produceTaskMaxNumber ; public static void main(String[] args) { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("Config.properties"); Properties p = new Properties(); try { p.load(inputStream); } catch (IOException e1) { e1.printStackTrace(); } // 构造一个线程池 ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3), new ThreadPoolExecutor.DiscardOldestPolicy()); for (int i = 1; i <= p.getProperty(produceTaskMaxNumber); i++) { try { String task = "task@ " + i; System.out.println("创建任务并提交到线程池中:" + task); threadPool.execute(new ThreadPoolTask(task)); Thread.sleep(p.getProperty.(produceTaskSleepTime)); } catch (Exception e) { e.printStackTrace(); } } } }