Executors创建线程池的几种方式以及使用
import lombok.experimental.Delegate; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class ThreadUtil { //维护一个单例线程
private static final ThreadUtil threadUtil = new ThreadUtil(); // 代理模式 这样可以直接调用父类中的方法
// public interface ExecutorService extends Executor
//public interface Executor { /**
* Executes the given command at some time in the future. The command
* may execute in a new thread, in a pooled thread, or in the calling
* thread, at the discretion of the {@code Executor} implementation.
*
* @param command the runnable task
* @throws RejectedExecutionException if this task cannot be
* accepted for execution
* @throws NullPointerException if command is null
*/
void execute(Runnable command);
} // 1.采用newCachedThreadPool创建线程池
@Delegate
public ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); //2.采用newFixedThreadPool创建线程池
@Delegate
public ExecutorService service = Executors.newFixedThreadPool(3); //3.采用newScheduledThreadPool 创建一个定长线程池 支持定时及周期性任务执行。
// 使用方法: ThreadUtil.getInstance().schedule(new TestThread(),3, TimeUnit.SECONDS);
@Delegate
public ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(2); //4.采用newSingleThreadExecutor 创建一个单线程化的线程池
@Delegate
public ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor(); public static ThreadUtil getInstance() {
return threadUtil;
} }
@Override
public String sendMsg() throws Exception { //把业务内容放在类中
ThreadUtil.getInstance().execute(new TestThread()); //或者这样直接写业务内容
ThreadUtil.getInstance().execute( () -> { System.out.println("222"); // 打印线程的内存地址
System.out.println(System.identityHashCode(Thread.currentThread())); System.out.println(Thread.currentThread().getName());
}
);
return "ok";
} private class TestThread implements Runnable{ @Override
public void run() {
System.out.println("111"); System.out.println(Thread.currentThread().getName()); System.out.println(System.identityHashCode(Thread.currentThread()));
}
}
Executors创建线程池的几种方式以及使用的更多相关文章
- 为什么阿里巴巴要禁用Executors创建线程池?
作者:何甜甜在吗 juejin.im/post/5dc41c165188257bad4d9e69 看阿里巴巴开发手册并发编程这块有一条:线程池不允许使用Executors去创建,而是通过ThreadP ...
- 为什么尽量不要使用Executors创建线程池
看阿里巴巴开发手册并发编程这块有一条:线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,通过源码分析禁用的原因. 线程池的优点 管理一组工作线程,通过线程池 ...
- [转]为什么阿里巴巴要禁用Executors创建线程池?
作者:何甜甜在吗 链接:https://juejin.im/post/5dc41c165188257bad4d9e69 来源:掘金 看阿里巴巴开发手册并发编程这块有一条:线程池不允许使用Executo ...
- 阿里不允许使用 Executors 创建线程池!那怎么使用,怎么监控?
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 五常大米好吃! 哈哈哈,是不你总买五常大米,其实五常和榆树是挨着的,榆树大米也好吃, ...
- java核心知识点学习----创建线程的第三种方式Callable和Future CompletionService
前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...
- java核心知识点----创建线程的第三种方式 Callable 和 Future CompletionService
前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...
- JAVA中创建线程池的五种方法及比较
之前写过JAVA中创建线程的三种方法及比较.这次来说说线程池. JAVA中创建线程池主要有两类方法,一类是通过Executors工厂类提供的方法,该类提供了4种不同的线程池可供使用.另一类是通过Thr ...
- Android 应用开发 之通过AsyncTask与ThreadPool(线程池)两种方式异步加载大量数据的分析与对比--转载
在加载大量数据的时候,经常会用到异步加载,所谓异步加载,就是把耗时的工作放到子线程里执行,当数据加载完毕的时候再到主线程进行UI刷新.在数据量非常大的情况下,我们通常会使用两种技术来进行异步加载,一 ...
- java 中创建线程有哪几种方式?
Java中创建线程主要有三种方式: 一.继承Thread类创建线程类 (1)定义Thread类的子类,并重写该类的run方法,该run方法的方法体就代表了线程要完成的任务.因此把run()方法称为执行 ...
随机推荐
- Eclispe最常用的几个快捷键
熟练使用快捷键可以在很大程度上提高我们的工作效率,Eclipse的快捷键很多,但是常用的也就那么几个,下面说下Eclispe最常用的几个快捷键: Eclipse的快捷键组合可在Eclipse按下ctr ...
- 在Mac上 python中使用tesseract OCR (Pytesser) 识别图片中的文字
仓库地址:https://github.com/RobinDavid/Pytesser brew install tesseract sudo pip install opencv-python 安装 ...
- linux 下的clock_gettime() 获取时间函数
#include <time.h> int clock_gettime(clockid_t clk_id, struct timespec* tp); 可以根据需要,获取不同要求的精确时间 ...
- .so相关总结
1.windows 中查看进程依赖那个dll,使用depends,linux使用ldd命令. 2.查看dll中有哪些导出函数windows使用dumpbin,linux使用objdump查看so中有哪 ...
- python线程、协程、I/O多路复用
目录: 并发多线程 协程 I/O多路复用(未完成,待续) 一.并发多线程 1.线程简述: 一条流水线的执行过程是一个线程,一条流水线必须属于一个车间,一个车间的运行过程就是一个进程(一个进程内至少一个 ...
- 关于Java中StringBuffer的capacity问题
从API查到capacity的作用是查看StringBuffer的容器容量是多少,刚开始纳闷这个跟length的区别在哪?试验了几次感觉有点不解.所以直接跟进源码分析. 直接通过new StringB ...
- python数据结构-如何实现用户的历史记录功能
如何实现用户的历史记录功能 使用collections中的deque from collections import deque dq = deque([], 5) dq.append(1) dq.a ...
- js前端使用jOrgChart插件实现组织架构图的展示
项目要做组织架构图,要把它做成自上而下的树形结构. 需要购买阿里云产品的,可以点击此链接购买,有红包优惠哦: https://promotion.aliyun.com/ntms/yunparter/i ...
- mysql查看当前执行线程_关闭当前的某些线程 show processlist_kill
每个与mysqld的连接都在一个独立的线程里运行,您可以使用SHOW PROCESSLIST语句查看哪些线程正在运行,并使用KILL thread_id语句终止一个线程. 如果您拥有SUPER权限,您 ...
- 网络请求(I)
NSURLSession 代理方法 有的时候,我们可能需要监听网络请求的过程(如下载文件需监听文件下载进度),那么就需要用到代理方法. #import "ViewController.h&q ...