天天看點

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();

                      }

                  }

              });

          }

      }

  }