线程池的使用
线程池
jdk1.5后官方提供线程池
创建一个线程类
/** * @Classname runnabledemo * @Description TODO * @Date 2020/11/11 11:59 * @Created by Administrator */ public class runnabledemo implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName()+" 正在执行..."); } }
创建一个测试类
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @Classname demo01 * @Description TODO * @Date 2020/11/11 10:14 * @Created by Administrator */ public class demo01 { public static void main(String[] args) { //1.创建线程池 ExecutorService executorService = Executors.newFixedThreadPool(2); //2.调用线程池中的线程执行指定的线程对象 executorService.submit(new runnabledemo()); executorService.submit(new runnabledemo()); executorService.submit(new runnabledemo()); //3.销毁线程池(不建议执行) executorService.shutdown(); } }