1 package com.thread.gaoji; 2 3 import java.util.concurrent.ExecutorService; 4 import java.util.concurrent.Executors; 5 6 //测试线程池 7 public class TestPool { 8 9 public static void main(String[] args) { 10 //1.创建服务,创建线程池 11 //newFixedThreadPool(10); 参数为线程池的大小 12 ExecutorService service = Executors.newFixedThreadPool(10); 13 14 //执行 15 service.execute(new MyThread()); 16 service.execute(new MyThread()); 17 service.execute(new MyThread()); 18 service.execute(new MyThread()); 19 20 //2.关闭连接 21 service.shutdown(); 22 } 23 } 24 25 class MyThread implements Runnable { 26 27 @Override 28 public void run() { 29 System.out.println(Thread.currentThread().getName()); 30 } 31 32 } 33 34 结果: 35 pool-1-thread-2 36 pool-1-thread-3 37 pool-1-thread-1 38 pool-1-thread-4
原文:https://www.cnblogs.com/duanfu/p/12260821.html