本文共 1223 字,大约阅读时间需要 4 分钟。
void execute(Runnable command)
执行任务/命令,没有返回值,一般用来执行 Runnable <T> Future<T> submit(Callable<T> task)
执行任务,有返回值,一般用来执行 Callable void shutdown()
关闭连接池下面通过代码来实现一个小案例:
package org.javaboy.juc;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * @Author: bai * @DateTime: 2020/6/7 21:51 */public class TestPool { public static void main(String[] args) { // step1.创建服务,创建线程池 // newFixedThreadPool 参数为:线程池大小 ExecutorService service = Executors.newFixedThreadPool(10); // step2.执行 service.execute(new MyThread()); service.execute(new MyThread()); service.execute(new MyThread()); // step3.关闭连接 service.shutdown(); }}class MyThread implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName()); }}
转载地址:http://xvqwi.baihongyu.com/