Java编程中线程池的基本概念和使用

 更新时间:2015年11月16日 08:54:20   投稿:goldensun  
这篇文章主要介绍了Java编程中线程池的基本概念和使用,多线程编程是使Java程序实现并发的一个重要手段,需要的朋友可以参考下

1 引入线程池的原因
  由于线程的生命周期中包括创建、就绪、运行、阻塞、销毁阶段,当我们待处理的任务数目较小时,我们可以自己创建几个线程来处理相应的任务,但当有大量的任务时,由于创建、销毁线程需要很大的开销,运用线程池这些问题就大大的缓解了。

2 线程池的使用
  我们只需要运用Executors类给我们提供的静态方法,就可以创建相应的线程池:

  public static ExecutorSevice newSingleThreadExecutor()

  public static ExecutorSevice newFixedThreadPool()

  public static ExecutorSevice newCachedThreadPool()

  newSingleThreadExecutor返回以个包含单线程的Executor,将多个任务交给此Exector时,这个线程处理完一个任务后接着处理下一个任务,若该线程出现异常,将会有一个新的线程来替代。

  newFixedThreadPool返回一个包含指定数目线程的线程池,如果任务数量多于线程数目,那么没有没有执行的任务必须等待,直到有任务完成为止。

  newCachedThreadPool根据用户的任务数创建相应的线程来处理,该线程池不会对线程数目加以限制,完全依赖于JVM能创建线程的数量,可能引起内存不足。

  我们只需要将待执行的任务放入run方法中即可,将Runnable接口的实现类交给线程池的execute方法,作为它的一个参数,如下所示:

Executor executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable(){
  public void run(){
    //执行的任务  
 }
}

  如果需要给任务传递参数,可以通过创建一个Runnable接口的实现类来完成。

3.实例
(1):newSingleThreadExecutor
MyThread.java

publicclassMyThread extends Thread {
  @Override
  publicvoid run() {
    System.out.println(Thread.currentThread().getName() + "正在执行。。。");
  }
}
TestSingleThreadExecutor.java
publicclassTestSingleThreadExecutor {
  publicstaticvoid main(String[] args) {
    //创建一个可重用固定线程数的线程池
    ExecutorService pool = Executors. newSingleThreadExecutor();
    //创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口
    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-1正在执行。。。
pool-1-thread-1正在执行。。。
pool-1-thread-1正在执行。。。
pool-1-thread-1正在执行。。。

(2):newFixedThreadPool
TestFixedThreadPool.Java

publicclass TestFixedThreadPool {
  publicstaticvoid main(String[] args) {
    //创建一个可重用固定线程数的线程池
    ExecutorService pool = Executors.newFixedThreadPool(2);
    //创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口
    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正在执行。。。
pool-1-thread-1正在执行。。。

(3): newCachedThreadPool
TestCachedThreadPool.java

publicclass TestCachedThreadPool {
  publicstaticvoid main(String[] args) {
    //创建一个可重用固定线程数的线程池
    ExecutorService pool = Executors.newCachedThreadPool();
    //创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口
    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):newScheduledThreadPool
TestScheduledThreadPoolExecutor.java

publicclass TestScheduledThreadPoolExecutor {
  publicstaticvoid main(String[] args) {
    ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
    exec.scheduleAtFixedRate(new Runnable() {//每隔一段时间就触发异常
           @Override
           publicvoid run() {
              //throw new RuntimeException();
              System.out.println("================");
           }
         }, 1000, 5000, TimeUnit.MILLISECONDS);
    exec.scheduleAtFixedRate(new Runnable() {//每隔一段时间打印系统时间,证明两者是互不影响的
           @Override
           publicvoid run() {
              System.out.println(System.nanoTime());
           }
         }, 1000, 2000, TimeUnit.MILLISECONDS);
  }
}

输出结果

================
8384644549516
8386643829034
8388643830710
================
8390643851383
8392643879319
8400643939383

相关文章

  • 详解如何使用MongoDB+Springboot实现分布式ID的方法

    详解如何使用MongoDB+Springboot实现分布式ID的方法

    这篇文章主要介绍了详解如何使用MongoDB+Springboot实现分布式ID的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • SpringBoot开发之整合Mybatis详解

    SpringBoot开发之整合Mybatis详解

    这篇文章主要介绍了SpringBoot开发之整合Mybatis详解,MyBatis是一个半自动的ORM框架,它允许我们通过编写SQL语句来操作数据库,使用MyBatis,我们可以通过定义映射文件(XML文件)或使用注解的方式将Java对象与数据库表进行映射,需要的朋友可以参考下
    2023-09-09
  • springmvc流程图以及配置解析

    springmvc流程图以及配置解析

    这篇文章主要介绍了springmvc流程图以及配置解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • JVM虚拟机性能监控与故障处理工具介绍

    JVM虚拟机性能监控与故障处理工具介绍

    这篇文章主要为大家介绍了JVM虚拟机性能监控与故障处理工具介绍,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • java服务自动停止原因查找方式

    java服务自动停止原因查找方式

    这篇文章主要介绍了java服务自动停止原因查找方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Springboot2.x结合Mabatis3.x下Hikari连接数据库报超时错误

    Springboot2.x结合Mabatis3.x下Hikari连接数据库报超时错误

    本文针对Springboot2.x与Mybatis3.x结合使用时,Hikari连接数据库出现超时错误的问题进行了深入分析,并提供了一系列有效的解决方法,感兴趣的可以了解一下
    2023-11-11
  • 详解SpringCloud的负载均衡

    详解SpringCloud的负载均衡

    这篇文章主要介绍了SpringCloud的负载均衡的相关资料,帮助大家更好的理解和学习使用SpringCloud,感兴趣的朋友可以了解下
    2021-03-03
  • spring-boot集成spring-security的oauth2实现github登录网站的示例

    spring-boot集成spring-security的oauth2实现github登录网站的示例

    本篇文章主要介绍了spring-boot集成spring-security的oauth2实现github登录网站的示例,非常具有实用价值,需要的朋友可以参考下
    2017-10-10
  • 使用Rhino让java执行javascript的方法实例

    使用Rhino让java执行javascript的方法实例

    这篇文章主要介绍了java使用Rhino执行javascript的方法,Rhino由Mozilla开发,是 JavaScript 一种基于Java的实现
    2013-12-12
  • Spring事务的开启原理详解

    Spring事务的开启原理详解

    这篇文章主要介绍了Spring事务的简单实现步骤,帮助大家更好的理解和学习使用spring,感兴趣的朋友可以了解下
    2021-03-03

最新评论