线程池
来源:互联网 发布:58同城网络兼职 编辑:程序博客网 时间:2024/05/16 14:16
所谓线程池就是多个线程封装在一起操作,线程池对任务进行操作的时候,有几种运行操作型式,单线程,无限多,限定长度,对线程池操作的核心就类和方法定义在java.util.concurrent包中,其中有两个比较核心 的接口:
1.普通的执行线程池定义:Interface ExecutorService
2.调度线程池:java.util.concurrent.ScheduledExecutorService
创建一个线程池可以使用java.util.concurrent.Executors类
。创建无大小的线程池:public static ExecutorService newCachedThreadPool()
。创建固定大小的线程池:public static ExecutorService newFixedThreadPool(int nThreads)
。单线程池:public static ScheduledExecutorService newSingleThreadScheduledExecutor()
。穿件定时调度池:public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
几种的实现demo
package com.wjx.sayHello;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class TestDemo {public static void main(String[] args) throws InterruptedException{/* * ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();for(int x=0;x<10;x++){//Thread.sleep(200);int temp=x;newCachedThreadPool.submit(()->{System.out.println(Thread.currentThread().getName()+"==="+temp);});}newCachedThreadPool.shutdown();*///ExecutorService newCachedThreadPool = Executors.newSingleThreadExecutor();//for(int x=0;x<10;x++){////Thread.sleep(200);//int temp=x;//newCachedThreadPool.submit(()->{//System.out.println(Thread.currentThread().getName()+"==="+temp);//});//}////newCachedThreadPool.shutdown();//ExecutorService newCachedThreadPool = Executors.newFixedThreadPool(4);//for(int x=0;x<10;x++){////Thread.sleep(200);//int temp=x;//newCachedThreadPool.submit(()->{//System.out.println(Thread.currentThread().getName()+"==="+temp);//});//}////newCachedThreadPool.shutdown();ScheduledExecutorService newCachedThreadPool = Executors.newScheduledThreadPool(1);for(int x=0;x<10;x++){//Thread.sleep(200);int temp=x;newCachedThreadPool.scheduleAtFixedRate(new Runnable(){@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"==="+temp);System.out.println("****************************************");}}, 3, 2, TimeUnit.SECONDS);}}}
阅读全文
0 0
- 线程与线程池
- 线程池 线程优先级
- Executor线程,线程池
- 线程、多线程、线程池
- IOS-线程、线程池
- Java线程:线程池
- 线程和线程池
- 线程、多线程、线程池
- 线程、多线程、线程池
- 线程(六)线程池
- ExecutorService(线程池)+线程
- 线程和线程池
- 线程&线程池 简略
- 线程和线程池
- 线程池 线程锁
- 线程、线程池总结
- 线程与线程池
- 线程、锁、线程池
- onresume 跳转时出现控件为空 是因为页面没有完全初始化
- iOS_新版iOS11 UITbleView适配的一些问题及解决方法
- 【ccf-201612-1】中间数
- 极乐技术周报(第二十六期)
- 海思3519编译opencv320库
- 线程池
- 基于vue-cli的vue项目之vuex的使用3-------action异步传参
- Spark SQL和Spark Thrift Server安装部署
- Pycharm2017 注册失败解决
- ios 获取导航栏、状态栏的高度
- resultMap跟resultType的区别
- 导航页
- 谈一谈String、StringBuffer和StringBuilder的区别
- 搭建git的局域网服务器