• 技术文章 >Java >java教程

    Java 线程池框架

    高洛峰高洛峰2017-02-11 16:52:46原创492
    本文主要介绍了Java 线程池框架的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧

    一、线程池结构图

    Java 线程池框架

    二、示例

    定义线程接口

    public class MyThread extends Thread {
     @Override
     publicvoid run() {
     System.out.println(Thread.currentThread().getName() + "正在执行");
     }
    }

    1:newSingleThreadExecutor

    ExecutorService pool = Executors. newSingleThreadExecutor();
     Thread t1 = new MyThread();
     Thread t2 = new MyThread();
     Thread t3 = new MyThread();
     //将线程放入池中进行执行
     pool.execute(t1);
     pool.execute(t2);
     pool.execute(t3);
     //关闭线程池
     pool.shutdown();

    输入结果:

    pool-1-thread-1正在执行
    pool-1-thread-1正在执行
    pool-1-thread-1正在执行

    2:newFixedThreadPool

    ExecutorService pool = Executors.newFixedThreadPool(3);
    Thread t1 = new MyThread();
     Thread t2 = new MyThread();
     Thread t3 = new MyThread();
     Thread t4 = new MyThread();
     Thread t5 = new MyThread();
     //将线程放入池中进行执行
     pool.execute(t1);
     pool.execute(t2);
     pool.execute(t3);
     pool.execute(t4);
     pool.execute(t5);
    pool.shutdown();

    输入结果:

    pool-1-thread-1正在执行
    pool-1-thread-2正在执行
    pool-1-thread-1正在执行
    pool-1-thread-2正在执行

    3 :newCachedThreadPool

    ExecutorService pool = Executors.newCachedThreadPool();
     Thread t1 = new MyThread();
     Thread t2 = new MyThread();
     Thread t3 = new MyThread();
     Thread t4 = new MyThread();
     Thread t5 = new MyThread();
     //将线程放入池中进行执行
     pool.execute(t1);
     pool.execute(t2);
     pool.execute(t3);
     pool.execute(t4);
     pool.execute(t5);
     //关闭线程池
     pool.shutdown();

    输入结果:

    pool-1-thread-2正在执行
    pool-1-thread-4正在执行
    pool-1-thread-3正在执行
    pool-1-thread-1正在执行
    pool-1-thread-5正在执行

    4 :ScheduledThreadPoolExecutor

    ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
    pool.scheduleAtFixedRate(new Runnable() {//每隔一段时间就触发异常
      @Override
      public void run() {
       //throw new RuntimeException();
       System.out.println("================");
      }
     }, 1000, 2000, TimeUnit.MILLISECONDS);
    pool.scheduleAtFixedRate(new Runnable() {//每隔一段时间打印系统时间,证明两者是互不影响的
      @Override
      public void run() {
       System.out.println("+++++++++++++++++");
      }
     }, 1000, 2000, TimeUnit.MILLISECONDS);

    输入结果:

    ================
    +++++++++++++++++
    +++++++++++++++++
    +++++++++++++++++

    三、线程池核心参数

    corePoolSize : 池中核心的线程数

    maximumPoolSize : 池中允许的最大线程数。

    keepAliveTime : 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。

    unit : keepAliveTime 参数的时间单位。

    workQueue : 执行前用于保持任务的队列。此队列仅保持由 execute方法提交的 Runnable任务。

    threadFactory : 执行程序创建新线程时使用的工厂。

    handler : 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序。

    ThreadPoolExecutor :Executors类的底层实现。

    3.1 任务排队机制

    SynchonousQueue: 同步队列,队列直接提交给线程执行而不保持它们,此时线程池通常是无界的

    LinkedBlockingQueue: 无界对列,当线程池线程数达到最大数量时,新任务就会在队列中等待执行,可能会造成队列无限膨胀

    ArrayBlockingQueue : 有界队列,有助于防止资源耗尽,一旦达到上限,可能会造成新任务丢失

    注意:

    newSingleThreadExecutor、newFixedThreadPool使用的是LinkedBlockingQueue

    newCachedThreadPool 使用的是 SynchonousQueue

    newScheduledThreadPool使用的是 DelayedWorkQueue

    3.2 线程执行流程

    Java 线程池框架

    3.3 线程大小确定:

    cpu密集型: 尽量少开线程,最佳线程数 Ncpu+1

    io密集型:多开线程,2Ncpu

    混合型:根据情况而定,可以拆分成io密集和cou密集

    更多Java 线程池框架相关文章请关注PHP中文网!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:Java 线程池 框架
    上一篇:Android RecyclerView加载两种布局的方法 下一篇:Java 实例 - 递归创建目录
    PHP编程就业班

    相关文章推荐

    • 详细解析Java反射机制原理和几种Class获取方式• 图文详解!什么是Java内存模型• 图文详解Java数据结构与算法• 带你搞懂JAVA反射机制(总结分享)• 深入解析JAVA中字符串常量池和缓冲池理解与作用

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网