本文共 729 字,大约阅读时间需要 2 分钟。
public class TestPool { public static void main(String[] args) { //1.创建服务,创建线程池 //newFixedThreadPool 参数为:线程池大小 ExecutorService service = Executors.newFixedThreadPool(10); //执行 service.execute(new MyThread()); service.execute(new MyThread()); service.execute(new MyThread()); service.execute(new MyThread()); //关闭连接 service.shutdown(); }}class MyThread implements Runnable{ @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName()); } }}
参数10为线程池的最大线程数
ExecutorService service = Executors.newFixedThreadPool(10);
用完之后记得关闭连接
service.shutdown();
转载地址:http://yseq.baihongyu.com/