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()方法称为执行 ...
随机推荐
- Linux命令更新系统时间,更新所有文件的时间(转)
https://blog.csdn.net/ccj2020/article/details/76026606
- 201621123049《java程序设计》第四周学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 继承 类型转换 覆盖 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. 2. 书面作业 1. ...
- php const常量 不能使用字符串连接符.链接
class jdpay extends pay { const BASE_URL = "https://mapi.jdpay.com/npp10/"; private $agree ...
- linux_rename命令用法
rename在man中的解释为: NAME rename - rename files SYNOPSIS rename [options] expression replacement file... ...
- SparkContext初始化之SparkScheduler初始化
- favorite learning link
Xpath https://www.cnblogs.com/chenshaoping/p/5540434.html Awk Sort https://www.cnblogs.com/chengmo/a ...
- Mac开发工具汇总
1: Json Parser Mac版 http://www.pc6.com/mac/180470.html
- 转载的web server实例
asp.net—web server模拟网上购物 2014-05-08 我来说两句 来源:asp.net—web server模拟网上购物 收藏 我要投稿 在学vb的时候学到了a ...
- 构建 Owin 中间件 来获取客户端IP地址
Not so long ago, we discussed on this blog the possible ways of retrieving the client’s IP address i ...
- appium元素获取
查看安卓手机的界面元素: 1.打开模拟器: 2.启动服务: 在cmd 输入: adb connect 127.0.0.1:62001 回车 可以输入 adb devices 查看启动状态 3.在a ...