Java通过Executors提供四种线程池,分别为:
 
1.newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
 
2.newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
3.newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
 
4.newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
 
 
直接上代码:

  1. import lombok.experimental.Delegate;
  2.  
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5.  
  6. public class ThreadUtil {
  7.  
  8. //维护一个单例线程
  9. private static final ThreadUtil threadUtil = new ThreadUtil();
  10.  
  11. // 代理模式 这样可以直接调用父类中的方法
  12. // public interface ExecutorService extends Executor
  13. //public interface Executor {
  14.  
  15. /**
  16. * Executes the given command at some time in the future. The command
  17. * may execute in a new thread, in a pooled thread, or in the calling
  18. * thread, at the discretion of the {@code Executor} implementation.
  19. *
  20. * @param command the runnable task
  21. * @throws RejectedExecutionException if this task cannot be
  22. * accepted for execution
  23. * @throws NullPointerException if command is null
  24. */
  25. void execute(Runnable command);
  26. }
  27.  
  28. // 1.采用newCachedThreadPool创建线程池
  29. @Delegate
  30. public ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
  31.  
  32. //2.采用newFixedThreadPool创建线程池
  33. @Delegate
  34. public ExecutorService service = Executors.newFixedThreadPool(3);
  35.  
  36. //3.采用newScheduledThreadPool 创建一个定长线程池 支持定时及周期性任务执行。
  37. // 使用方法: ThreadUtil.getInstance().schedule(new TestThread(),3, TimeUnit.SECONDS);
  38. @Delegate
  39. public ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(2);
  40.  
  41. //4.采用newSingleThreadExecutor 创建一个单线程化的线程池
  42. @Delegate
  43. public ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
  44.  
  45. public static ThreadUtil getInstance() {
  46. return threadUtil;
  47. }
  48.  
  49. }
  1. @Override
  2. public String sendMsg() throws Exception {
  3.  
  4. //把业务内容放在类中
  5. ThreadUtil.getInstance().execute(new TestThread());
  6.  
  7. //或者这样直接写业务内容
  8. ThreadUtil.getInstance().execute( () -> {
  9.  
  10. System.out.println("222");
  11.  
  12. // 打印线程的内存地址
  13. System.out.println(System.identityHashCode(Thread.currentThread()));
  14.  
  15. System.out.println(Thread.currentThread().getName());
  16. }
  17. );
  18. return "ok";
  19. }
  20.  
  21. private class TestThread implements Runnable{
  22.  
  23. @Override
  24. public void run() {
  25. System.out.println("111");
  26.  
  27. System.out.println(Thread.currentThread().getName());
  28.  
  29. System.out.println(System.identityHashCode(Thread.currentThread()));
  30. }
  31. }

Executors创建线程池的几种方式以及使用的更多相关文章

  1. 为什么阿里巴巴要禁用Executors创建线程池?

    作者:何甜甜在吗 juejin.im/post/5dc41c165188257bad4d9e69 看阿里巴巴开发手册并发编程这块有一条:线程池不允许使用Executors去创建,而是通过ThreadP ...

  2. 为什么尽量不要使用Executors创建线程池

    看阿里巴巴开发手册并发编程这块有一条:线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,通过源码分析禁用的原因. 线程池的优点 管理一组工作线程,通过线程池 ...

  3. [转]为什么阿里巴巴要禁用Executors创建线程池?

    作者:何甜甜在吗 链接:https://juejin.im/post/5dc41c165188257bad4d9e69 来源:掘金 看阿里巴巴开发手册并发编程这块有一条:线程池不允许使用Executo ...

  4. 阿里不允许使用 Executors 创建线程池!那怎么使用,怎么监控?

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 五常大米好吃! 哈哈哈,是不你总买五常大米,其实五常和榆树是挨着的,榆树大米也好吃, ...

  5. java核心知识点学习----创建线程的第三种方式Callable和Future CompletionService

    前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...

  6. java核心知识点----创建线程的第三种方式 Callable 和 Future CompletionService

    前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...

  7. JAVA中创建线程池的五种方法及比较

    之前写过JAVA中创建线程的三种方法及比较.这次来说说线程池. JAVA中创建线程池主要有两类方法,一类是通过Executors工厂类提供的方法,该类提供了4种不同的线程池可供使用.另一类是通过Thr ...

  8. Android 应用开发 之通过AsyncTask与ThreadPool(线程池)两种方式异步加载大量数据的分析与对比--转载

     在加载大量数据的时候,经常会用到异步加载,所谓异步加载,就是把耗时的工作放到子线程里执行,当数据加载完毕的时候再到主线程进行UI刷新.在数据量非常大的情况下,我们通常会使用两种技术来进行异步加载,一 ...

  9. java 中创建线程有哪几种方式?

    Java中创建线程主要有三种方式: 一.继承Thread类创建线程类 (1)定义Thread类的子类,并重写该类的run方法,该run方法的方法体就代表了线程要完成的任务.因此把run()方法称为执行 ...

随机推荐

  1. MUI - 复选框、单选框、使用js获取选择值

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  2. nginx 禁止恶意域名解析

    server { listen default_server; server_name _; ssl on; ssl_certificate /etc/nginx/cert/aaaa.pem; ssl ...

  3. ML.NET 0.9特性简介

    ML.NET 0.9已于上周发布,距离上次0.8版本的发布只有一个多月,此次增加的新特性主要包括特征贡献计算,模型可解释性增强,ONNX转换对GPU的支持,Visual Studio ML.NET项目 ...

  4. sql数据库光标变成黑快怎么回事?

    可能是因为你按到了insert键啦,你再按一下insert键应该就可以啦. 光标变成块状说明当前是覆盖模式.光标变成竖条状说明当前是插入模式.

  5. SparkML之推荐引擎(二)---推荐模型评估

    本文内容和代码是接着上篇文章来写的,推荐先看一下哈~ 我们上一篇文章是写了电影推荐的实现,但是推荐内容是否合理呢,这就需要我们对模型进行评估 针对推荐模型,这里根据 均方差 和 K值平均准确率 来对模 ...

  6. postgres密码修改

    . 修改PostgreSQL数据库默认用户postgres的密码 PostgreSQL数据库创建一个postgres用户作为数据库的管理员,密码随机,所以需要修改密码,方式如下: 步骤一:登录Post ...

  7. 日志采集器windows客户端的配置释义

    <Extension json> Module xm_json </Extension> <Extension charconv> Module xm_charco ...

  8. Eclipse Error Reporting Welcome to the Eclipse Error Reporting Service.Do you want to help Eclipse? Enable Disable

    在开发的时候,使用Eclipse IDE,提示如下信息, 这是Eclipse的错误报告,如果不想发送,可以关闭掉,关闭方法: 选择Preferences -> General -> Err ...

  9. [PHP] swoole在daemonize模式下,chdir失效问题

    swoole version: 1.9.6 其实跟swoole的版本无关,因为原代码体系,fpm模式下,在启动的时候,是使用 chdir 函数改变了当前目录的,而其它代码在做类的自动加载的时候,都是写 ...

  10. ArchLinux安装Sublime Text 3

    安装方法: 在 /etc/pacman.conf中添加 [archlinuxcn] SigLevel = Optional TrustAll Server = http://repo.archlinu ...