天天看点

java线程池

线程池的作用:

         提供系统效率;如果每个请求到达就创建一个线程,开销是相当大的,服务器创建和销毁线程花费的时间和系统资源都相当大;如果线程数量太多,线程之间的切换也会消耗大量时间和资源。通过对多个任务重复使用线程,那么线程创建和销毁的开销就分摊到了多个任务上,平均每个任务所花费的时间和资源就少了很多。

1、  newCachedThreadPool

  创建可缓存线程池,如果线程池的大小超过处理需要的大小,可灵活回收空闲线程,如果不能回收,就创建线程,回收的线程可以用作执行新的任务

 

  import java.util.concurrent.ExecutorService;

  import java.util.concurrent.Executors;

 

  public class CachedThreadPool {

 

      @Override

      public void run() {

          ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

          for(int i = 0; i < 10; i++) {

              final int index = i;

              try {

                  Thread.sleep(index * 1000);

              } catch (InterruptedException e) {

                  e.printStackTrace();

              }

              cachedThreadPool.execute(new Runnable() {

                  @Override

                  public void run() {

                      System.out.println(index);

                  }

              });

          }

      }

  }      

2、  newFixedThreadPool

  创建定长线程,可控制线程的最大并发数,超出的线程需在队列中等待

  import java.util.concurrent.ExecutorService;

  import java.util.concurrent.Executors;

 

  public class FixedThreadPool {

      @Override

      public void run() {

          ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

          for (int i = 0; i < 10; i++) {

              final int index = i;

              fixedThreadPool.execute(new Runnable() {

                  @Override

                  public void run() {

                      try {

                          System.out.println(index);

                          Thread.sleep(2000);

                      } catch (InterruptedException e) {

                          e.printStackTrace();

                      }

                  }

              });

          }

      }

  }      

3、  newScheduledThreadPool

  创建定长线程,支持按照设定的时间延迟执行或者周期性执行

  

  import java.util.concurrent.Executors;

  import java.util.concurrent.ScheduledExecutorService;

  import java.util.concurrent.TimeUnit;

 

  public class ScheduledThreadPool {

      @Override

      public void run() {

          ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);

          scheduledThreadPool.scheduleAtFixedRate(new Runnable() {

              @Override

              public void run() {

                  System.out.println("3 seconds delay");

              }

          }, 1, 3, TimeUnit.SECONDS);

      }

  }      

4、  newSingleThreadPool

  单线程化的线程池,所有的任务的执行顺序按照指定的顺序执行(FIFO、LIFO、优先级等)

  import java.util.concurrent.ExecutorService;

  import java.util.concurrent.Executors;

 

  public class SingleThreadPool {

      @Override

      public void run() {

          ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();

          for (int i = 0 ; i < 10; i++) {

              final int index = i;

              singleThreadPool.execute(new Runnable() {

                  @Override

                  public void run() {

                      try {

                          System.out.println(index);

                          Thread.sleep(2000);

                      } catch (InterruptedException e) {

                          e.printStackTrace();

                      }

                  }

              });

          }

      }

  }