當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Executors newScheduledThreadPool()用法及代碼示例


Executors 類的 newScheduledThreadPool() 方法創建了一個線程池,該線程池可以安排命令在給定延遲後運行或定期執行。

用法

Public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)

參數

corePoolSize - 要保留在池中的線​​程數,即使它們處於空閑狀態

threadFactory - 執行程序創建新線程時使用的工廠

返回

新創建的調度線程池

拋出

NullPointerException

IllegalArgumentException

例子1

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorsnewSheduledThreadPoollExample1  {
	
   public static void main(final String[] arguments) throws InterruptedException {
       
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool( 3 );
       ThreadFactory ThreadFactory = Executors.defaultThreadFactory();
        System.out.println(scheduler.getClass());
      ExecutorService excr = Executors.newCachedThreadPool(ThreadFactory);
      ThreadPoolExecutor mypool = (ThreadPoolExecutor) excr;
      System.out.println("size of mypool:" + mypool.getPoolSize());
    
      excr.submit(new Threadimpl());
      
      excr.shutdown();
   }  

   static class Threadimpl implements Runnable {

      public void run() {
         
         try {
            Long num = (long) (Math.random() / 30);
            System.out.println("Thread Name:" +Thread.currentThread().getName());
               TimeUnit.SECONDS.sleep(num);
            System.out.println("after sleep Thread Name:" +Thread.currentThread().getName());
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
}

輸出:

class java.util.concurrent.ScheduledThreadPoolExecutor
size of mypool:0
Thread Name:pool-2-thread-1
after sleep Thread Name:pool-2-thread-1

例子2

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorsnewSheduledThreadPoollExample2  {
	
   public static void main(final String[] arguments) throws InterruptedException {
       
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool( 3 );
       ThreadFactory ThreadFactory = Executors.defaultThreadFactory();
        System.out.println(scheduler.getClass());
      ExecutorService excr = Executors.newCachedThreadPool(ThreadFactory);
      ThreadPoolExecutor mypool = (ThreadPoolExecutor) excr;
      System.out.println("size of mypool:" + mypool.getPoolSize());
      
               System.out.println(scheduler.isShutdown());
                System.out.println(scheduler.getClass());
      excr.shutdown();
   }  

  
}

輸出:

class java.util.concurrent.ScheduledThreadPoolExecutor
size of mypool:0
false
class java.util.concurrent.ScheduledThreadPoolExecutor




相關用法


注:本文由純淨天空篩選整理自 Java Executors newScheduledThreadPool() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。