线程池
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.bmabk.com/index.php/post/187686.html