zoukankan      html  css  js  c++  java
  • java 多线程 4 线程池

    系统启动一个新线程的成本是比较高的,因为它涉及到与操作系统的交互。在这种情况下,使用线程池可以很好的提供性能,尤其是当程序中需要创建大量生存期很短暂的线程时,更应该考虑使用线程池。

    数据库连接池类似的是,线程池在系统启动时即创建大量空闲的线程,程序将一个Runnable对象传给线程池,线程池就会启动一条线程来执行该对象的run方法,当run方法执行结束后,该线程并不会死亡,而是再次返回线程池中成为空闲状态,等待执行下一个Runnable对象的run方法。

    除此之外,使用线程池可以有效地控制系统中并发线程的数量,但系统中包含大量并发线程时,会导致系统性能剧烈下降,甚至导致JVM崩溃。而线程池的最大线程数参数可以控制系统中并发的线程不超过此数目。

    在JDK1.5之前,开发者必须手动的实现自己的线程池,从JDK1.5之后,Java内建支持线程池。

    与多线程并发的所有支持的类都在java.lang.concurrent包中。我们可以使用里面的类更加的控制多线程的执行。

    一、Executors类

    JDK1.5中提供Executors工厂类来产生连接池,该工厂类中包含如下的几个静态工程方法来创建连接池:

    1、public static ExecutorService newFixedThreadPool(int nThreads):创建一个可重用的、具有固定线程数的线程池。

    2、public static ExecutorService newSingleThreadExecutor():创建一个只有单线程的线程池,它相当于newFixedThreadPool方法是传入的参数为1

    3、public static ExecutorService newCachedThreadPool():创建一个具有缓存功能的线程池,系统根据需要创建线程,这些线程将会被缓存在线程池中。

    4、public static ScheduledExecutorService newSingleThreadScheduledExecutor:创建只有一条线程的线程池,他可以在指定延迟后执行线程任务

    5、public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize):创建具有指定线程数的线程池,它可以再指定延迟后执行线程任务,corePoolSize指池中所保存的线程数,即使线程是空闲的也被保存在线程池内。

    上面的几个方法都有一个重载的方法,多传入一个ThreadFactory参数的重载方法,使用的比较少。

    二、ExecutorService类

    可以看到上面的5个方法中,前面3个方法的返回值都是一个ExecutorService对象。该ExecutorService对象就代表着一个尽快执行线程的线程池(只要线程池中有空闲线程立即执行线程任务),程序只要将一个Runnable对象或Callable对象提交给该线程池即可,该线程就会尽快的执行该任务。

    ExecutorService有几个重要的方法:

    方法摘要
     boolean isShutdown() 
              如果此执行程序已关闭,则返回 true
     boolean isTerminated() 
              如果关闭后所有任务都已完成,则返回 true
     void shutdown() 
              启动一次顺序关闭,执行以前提交的任务,但不接受新任务。
     List<Runnable> shutdownNow() 
              试图停止所有正在执行的活动任务,暂停处理正在等待的任务,并返回等待执行的任务列表。
    <T> Future<T>
    submit(Callable<T> task) 
              提交一个返回值的任务用于执行,返回一个表示任务的未决结果的 Future。
     Future<?> submit(Runnable task) 
              提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。
    <T> Future<T>
    submit(Runnable task, T result) 
              提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。

     

    更详细的参考JDK API文档。

    submit方法是对 Executor接口execute方法的更好的封装,建议使用submit方法。

    三、ScheduleExecutorService类

    在上面的5个方法中,后面2个方法的返回值都是一个ScheduleExecutorService对象。ScheduleExecutorService代表可在指定延迟或周期性执行线程任务的线程池。

    ScheduleExecutorService类是ExecutorService类的子类。所以,它里面也有直接提交任务的submit方法,并且新增了一些延迟任务处理的方法:

    方法摘要
    <V> ScheduledFuture<V>
    schedule(Callable<V> callable, long delay, TimeUnit unit) 
              创建并执行在给定延迟后启用的 ScheduledFuture。
     ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) 
              创建并执行在给定延迟后启用的一次性操作。
     ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 
              创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
     ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) 
              创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。

     

    下面看看线程池的简单使用:

    1、固定大小的线程池:

    [java] view plain copy
     
    1. package com.tao.test;  
    2.   
    3. import java.util.concurrent.ExecutorService;  
    4. import java.util.concurrent.Executors;  
    5.   
    6. public class PoolTest {  
    7.     public static void main(String[] args) {  
    8.         ExecutorService pool=Executors.newFixedThreadPool(5);//创建一个固定大小为5的线程池  
    9.         for(int i=0;i<7;i++){  
    10.             pool.submit(new MyThread());  
    11.         }  
    12.         pool.shutdown();  
    13.     }  
    14. }  
    15. class MyThread extends Thread{  
    16.     @Override  
    17.     public void run() {  
    18.             System.out.println(Thread.currentThread().getName()+"正在执行。。。");  
    19.     }  
    20. }  

    输出结果:

    [java] view plain copy
     
    1. pool-1-thread-1正在执行。。。  
    2. pool-1-thread-3正在执行。。。  
    3. pool-1-thread-2正在执行。。。  
    4. pool-1-thread-4正在执行。。。  
    5. pool-1-thread-4正在执行。。。  
    6. pool-1-thread-5正在执行。。。  
    7. pool-1-thread-1正在执行。。。  

    可以看到虽然我们呢创建了7个MyThread线程对象,但是由于受线程池的大小限制,只是开启了5个线程,这样就减少了并发线程的数量。

    2、单任务线程池:

    [java] view plain copy
     
    1. public class PoolTest {  
    2.     public static void main(String[] args) {  
    3.         ExecutorService pool=Executors.newSingleThreadExecutor();//创建一个单线程池  
    4.         for(int i=0;i<7;i++){  
    5.             pool.submit(new MyThread());  
    6.         }  
    7.         pool.shutdown();  
    8.     }  
    9. }  


    输出结果:

    [java] view plain copy
     
    1. pool-1-thread-1正在执行。。。  
    2. pool-1-thread-1正在执行。。。  
    3. pool-1-thread-1正在执行。。。  
    4. pool-1-thread-1正在执行。。。  
    5. pool-1-thread-1正在执行。。。  
    6. pool-1-thread-1正在执行。。。  
    7. pool-1-thread-1正在执行。。。  


    可以看到,线程池只开启了一个线程。

    3、创建可变尺寸的线程池

    [java] view plain copy
     
    1. public class PoolTest {  
    2.     public static void main(String[] args) {  
    3.         ExecutorService pool=Executors.newCachedThreadPool();  
    4.         for(int i=0;i<5;i++){  
    5.             pool.submit(new MyThread());  
    6.         }  
    7.         pool.shutdown();  
    8.     }  
    9. }  


    看输出结果:

    [java] view plain copy
     
    1. pool-1-thread-1正在执行。。。  
    2. pool-1-thread-3正在执行。。。  
    3. pool-1-thread-2正在执行。。。  
    4. pool-1-thread-4正在执行。。。  
    5. pool-1-thread-5正在执行。。。  


    可以看到,我们没有限制线程池的大小,但是它会根据需求而创建线程。

    4、延迟线程池

    [java] view plain copy
     
    1. public class PoolTest {  
    2.     public static void main(String[] args) {  
    3.         ScheduledExecutorService pool=Executors.newScheduledThreadPool(6);  
    4.         for(int i=0;i<4;i++){  
    5.             pool.submit(new MyThread());  
    6.         }  
    7.           
    8.         pool.schedule(new MyThread(), 1000, TimeUnit.MILLISECONDS);  
    9.         pool.schedule(new MyThread(), 1000, TimeUnit.MILLISECONDS);  
    10.         pool.shutdown();  
    11.     }  
    12. }  

    输出结果:

    [java] view plain copy
     
    1. pool-1-thread-1正在执行。。。  
    2. pool-1-thread-3正在执行。。。  
    3. pool-1-thread-2正在执行。。。  
    4. pool-1-thread-4正在执行。。。  
    5. pool-1-thread-6正在执行。。。  
    6. pool-1-thread-1正在执行。。。  

    可以明显看到,最后两个线程不是立即执行,而是延迟了1秒在执行的。

    5、单任务延迟线程池

    [java] view plain copy
     
    1. public class PoolTest {  
    2.     public static void main(String[] args) {  
    3.         ScheduledExecutorService pool=Executors.newSingleThreadScheduledExecutor();  
    4.         for(int i=0;i<4;i++){  
    5.             pool.submit(new MyThread());  
    6.         }  
    7.           
    8.         pool.schedule(new MyThread(), 1000, TimeUnit.MILLISECONDS);  
    9.         pool.schedule(new MyThread(), 1000, TimeUnit.MILLISECONDS);  
    10.         pool.shutdown();  
    11.     }  
    12. }  

    上面我们使用的是JDK帮我封装好的线程池,我们也可以自己定义线程池,查看源码,我们发现,Excutors里面的获得线程的静态方法,内部都是调用ThreadPoolExecutor的构造方法。比如:

    [java] view plain copy
     
    1. public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {  
    2.     return new ThreadPoolExecutor(nThreads, nThreads,  
    3.                                   0L, TimeUnit.MILLISECONDS,  
    4.                                   new LinkedBlockingQueue<Runnable>(),  
    5.                                   threadFactory);  
    6. }  

    可以看到,它是通过调用ThreadPoolExecutor的构造方法来返回一个线程池的。所以,我们也可以自己手动的调用ThreadPoolExecutor的各种构造方法,来定义自己的线程池规则,不过一般情况下,使用自带的线程池就够了,不需要自己来实现。

  • 相关阅读:
    204. Count Primes (Integer)
    203. Remove Linked List Elements (List)
    202. Happy Number (INT)
    201. Bitwise AND of Numbers Range (Bit)
    200. Number of Islands (Graph)
    199. Binary Tree Right Side View (Tree, Stack)
    198. House Robber(Array; DP)
    191. Number of 1 Bits (Int; Bit)
    190. Reverse Bits (Int; Bit)
    189. Rotate Array(Array)
  • 原文地址:https://www.cnblogs.com/diegodu/p/6257210.html
Copyright © 2011-2022 走看看