1.线程池是用来存储线程的容器
2.Executors.newFixedThreadPool(int n);创建线程池,并且设置线程池的容量为n
3.submit开启线程
4.会返回一个对象future
5.可以通过future.get()获取线程值
public class ThreadPoolTest { public static void main(String[] args) { //1.创建线程池对象,规定有几个线程,如果线程满了,就处于等待状态 ExecutorService service = Executors.newFixedThreadPool(3); //2.提交线程 service.submit(new Demo_02Runnable()); service.submit(new Demo_02Runnable()); service.submit(new Demo_02Runnable()); Future<?> future = service.submit(new Demo_02Runnable()); try { System.out.println(future.get());//返回null,因为实现的是Runnable接口,run方法返回值为void } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } //3.关闭 service.shutdown(); } }