php源码网站后台面密码忘了在哪能找回密码,2022网页游戏排行榜前十名,厦门旅游网站设计,中国核工业华兴建设有限公司网站线程池
提前创建多个线程放入线程池中#xff0c;使用时直接获取#xff0c;使用完直接放入池中#xff1b;可以避免频繁创建销毁#xff0c;实现重复利用#xff0c;类似生活中的公共交通工具。好处#xff1a;提高相应速度#xff1b;降低资源消耗#xff1b;便于线…线程池
提前创建多个线程放入线程池中使用时直接获取使用完直接放入池中可以避免频繁创建销毁实现重复利用类似生活中的公共交通工具。好处提高相应速度降低资源消耗便于线程管理APIExecutorService和Executors
package jingcheng.test.gaoji;import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;//测试线程池
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.execute(new MyThread());service.execute(new MyThread());//2、关闭连接service.shutdown();}}class MyThread implements Runnable{Overridepublic void run() {System.out.println(Thread.currentThread().getName());}
}pool-1-thread-1 pool-1-thread-2 pool-1-thread-3 pool-1-thread-4 pool-1-thread-5 pool-1-thread-6