您好,欢迎访问一九零五行业门户网

Java Executors中的四种线程池是什么

1、线程池说明
newcachedthreadpool创建缓存线程池,如果线程池的长度超过处理需要,则可以灵活回收空闲线程,如果不能回收,则可以创建新的线程。
newfixedthreadpool创建一个定长的线程池,可以控制线程的并发数,超过的线程在队列中等待。
newscheduledthreadpool建立固定长线程池,支持定时和周期任务的执行。
newsinglethreadexecutor创建一个单线程化的线程池,只能用唯一的工作线程执行任务,保证所有任务按指定顺序执行。
2、实例
class threaddemo extends thread {     @override     public void run() {         system.out.println(thread.currentthread().getname() + 正在执行);     } } class testfixedthreadpool {         public static void main(string[] args) {         //创建一个可重用固定线程数的线程池         executorservice pool = executors.newfixedthreadpool(2);         //创建实现了runnable接口对象,thread对象当然也实现了runnable接口         thread t1 = new threaddemo();         thread t2 = new threaddemo();         thread t3 = new threaddemo();         thread t4 = new threaddemo();         thread t5 = new threaddemo();         //将线程放入池中进行执行         pool.execute(t1);         pool.execute(t2);         pool.execute(t3);         pool.execute(t4);         pool.execute(t5);         //关闭线程池         pool.shutdown();         }         }
以上就是java executors中的四种线程池是什么的详细内容。
其它类似信息

推荐信息