线程池

线程池在系统启动时即创建大量空闲的线程,程序将一个Runnable对象传给线程池,线程池就会启动一天线程来执行该对象的run方法 ,放run方法执行结束后,该线程并不会死亡,而是返回线程池中成为空闲状态,等待执行下一个Runnable对象的run方法

还可以控制系统中并发的线程数量

 1 package com.thread;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 
 6 public class ThreadPoolTest {
 7     public static void main(String[] args) {
 8         ExecutorService poolExecutorService=Executors.newFixedThreadPool(6);
 9         poolExecutorService.submit(new TestThread());
10         poolExecutorService.submit(new TestThread());
11         poolExecutorService.shutdown();
12     }
13 }
14 
15 class TestThread implements Runnable {
16     public void run() {
17         for (int i = 0; i < 10; i++) {
18             System.out.println(Thread.currentThread().getName() + i);
19         }
20     }
21 }
View Code

 

posted @ 2013-09-18 17:06  liutoutou  阅读(157)  评论(0编辑  收藏  举报