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(); } }
原文:https://www.cnblogs.com/sun-10387834/p/13958066.html