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()方法称为执行 ...
随机推荐
- allegro画电路板
出图: 选择save all checked导出film_setup.txt: 选择replace导入film_setup.txt: 丝印层选择silkscreen,但是silkscreen和asse ...
- 洛谷P3376 【模板】网络最大流
题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...
- linux vue uwsgi nginx 部署路飞学城 安装 vue
vue+uwsgi+nginx部署路飞学城 有一天,老男孩的苑日天给我发来了两个神秘代码,听说是和mjj的结晶 超哥将这两个代码,放到了一个网站上,大家可以自行下载 路飞学城django代码#这个代码 ...
- awt
public class MouseTest extends Frame{ private static final long serialVersionUID = 54376853365952763 ...
- gcc update in centos to 6.3 by scl
CentOS 7虽然已经出了很多年了,但依然会有很多人选择安装CentOS 6,CentOS 6有些依赖包和软件都比较老旧,如今天的主角gcc编译器,CentOS 6的gcc版本为4.4,CentOS ...
- TCP/IP协议 模型
OSI的来源 OSI(Open System Interconnect),即开放式系统互联. 一般都叫OSI参考模型,是ISO(国际标准化组织)组织在1985年研究的网络互连模型. ISO为了更好的 ...
- 查看文件内容 cat , tac
cat 文件名字tac 文件名字 -- 倒序查看文件内容
- 安装WIN7/WIN10上的 CPU版本的TensorFlow
随手记 ancaconda Anaconda2-5.0.1-Windows-x86_64(python3.5 ancaconda python-3.5.2-amd64 安装TensorFlow的时候自 ...
- cocos2d-x 贡献一个oss上传脚本
平常写前端项目和H5游戏时特别频繁的一个操作就是上传到oss上,特别浪费时间.所以用ali-oss写了一个脚本.配置属性后直接npm run oss就能上传到oss上了.再也不需要手动操作.现在是脚本 ...
- 使用COE脚本绑定SQL Profile
日常运维中,经常会遇到需要绑定好的执行计划的场景. 简单来说,就是将一个sql_id绑定好的plan_hash_value.如果没有使用到绑定变量,还需要把force_match设置为true. 用到 ...