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()方法称为执行 ...
随机推荐
- MUI - 复选框、单选框、使用js获取选择值
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- nginx 禁止恶意域名解析
server { listen default_server; server_name _; ssl on; ssl_certificate /etc/nginx/cert/aaaa.pem; ssl ...
- ML.NET 0.9特性简介
ML.NET 0.9已于上周发布,距离上次0.8版本的发布只有一个多月,此次增加的新特性主要包括特征贡献计算,模型可解释性增强,ONNX转换对GPU的支持,Visual Studio ML.NET项目 ...
- sql数据库光标变成黑快怎么回事?
可能是因为你按到了insert键啦,你再按一下insert键应该就可以啦. 光标变成块状说明当前是覆盖模式.光标变成竖条状说明当前是插入模式.
- SparkML之推荐引擎(二)---推荐模型评估
本文内容和代码是接着上篇文章来写的,推荐先看一下哈~ 我们上一篇文章是写了电影推荐的实现,但是推荐内容是否合理呢,这就需要我们对模型进行评估 针对推荐模型,这里根据 均方差 和 K值平均准确率 来对模 ...
- postgres密码修改
. 修改PostgreSQL数据库默认用户postgres的密码 PostgreSQL数据库创建一个postgres用户作为数据库的管理员,密码随机,所以需要修改密码,方式如下: 步骤一:登录Post ...
- 日志采集器windows客户端的配置释义
<Extension json> Module xm_json </Extension> <Extension charconv> Module xm_charco ...
- Eclipse Error Reporting Welcome to the Eclipse Error Reporting Service.Do you want to help Eclipse? Enable Disable
在开发的时候,使用Eclipse IDE,提示如下信息, 这是Eclipse的错误报告,如果不想发送,可以关闭掉,关闭方法: 选择Preferences -> General -> Err ...
- [PHP] swoole在daemonize模式下,chdir失效问题
swoole version: 1.9.6 其实跟swoole的版本无关,因为原代码体系,fpm模式下,在启动的时候,是使用 chdir 函数改变了当前目录的,而其它代码在做类的自动加载的时候,都是写 ...
- ArchLinux安装Sublime Text 3
安装方法: 在 /etc/pacman.conf中添加 [archlinuxcn] SigLevel = Optional TrustAll Server = http://repo.archlinu ...