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()方法称为执行 ...
随机推荐
- git使用记录_备忘
---恢复内容开始--- 一.将本地的文件上传到git 1.cd 本地文件目录 2.git init git init 命令使git命令可以管理当前的目录 3.git remote add origi ...
- 概率DP求解例题
1,逆推状态:山东省赛2013年I题 Problem I: The number of steps Description Mary stands in a strange maze, the maz ...
- mysql in 排序 也可以按in里面的顺序来排序
SQL: select * from table where id IN (3,9,6);这样的情况取出来后,其实,id还是按3,6,9,排序的,但如果我们真要按IN里面的顺序排序怎么办?SQL能不能 ...
- 【Python全栈-后端开发】Django进阶1-分页
Django[进阶篇-1 ]分页 分页 一.Django内置分页 from django.core.paginator import Paginator, EmptyPage, PageNotAnIn ...
- Linux Input子系统
先贴代码: //input.c int input_register_handler(struct input_handler *handler) { //此处省略很多代码 list_for_each ...
- kubectl批量删除pvc
#!/bin/bashkubectl get pvc |grep hub > tmp.txtcat tmp.txt |awk '{split($0,a," ");print ...
- 九、使用多线程——NSThread,GCD和NSOperation
概述 早上起床,你先打开洗衣机,然后用热水把泡面泡上,接着打开电脑开启一天的码农生活.其中“洗衣服”.“泡泡面”和“码代码”3个任务(线程)同时进行,这就是多线程.网上有许多关于多线程的经典解释,此处 ...
- 家庭记账本之微信小程序(三)
继上篇注册阶段后,经过查阅资料学习后,以下介绍开发阶段 1.登录微信公众平台就能在菜单“开发”---“基本配置”中看到小程序的AppID了. 小程序的 AppID 相当于小程序平台的一个身份证,后续你 ...
- Linux平台 Oracle 18c RAC安装Part1:准备工作
一.实施前期准备工作 1.1 服务器安装操作系统 1.2 Oracle安装介质 1.3 共享存储规划 1.4 网络规范分配 二.安装前期准备工作 2.1 各节点系统时间校对 2.2 各节点关闭防火墙和 ...
- 记使用talend从oracle抽取数据时,数字变为0的问题
数据源为oracle,字段类型为number. 发现通过mainline连接到一个logrow控件,输入的该字段的值为0 经过多次测试还是没发现有什么规律. 通过查看代码发现有这一句内容. if (r ...