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


Java Executor newWorkStealingPool()用法及代碼示例


Executors 類的 newWorkStealingPool() 方法創建一個 work-stealing 線程池,使用可用處理器的數量作為其目標並行度級別。

用法

public static ExecutorService newWorkStealingPool()
public static ExecutorService newWorkStealingPool(int parallelism)

參數

並行度 - 目標並行度級別

返回

新創建的線程池

拋出

IllegalArgumentException

例子1

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

public class ExecutorsnewWorkStealingPoolExample1  {
	
   public static void main(final String[] arguments) throws InterruptedException {
      ExecutorService excr = Executors.newWorkStealingPool();
      ThreadPoolExecutor mypool = (ThreadPoolExecutor)  Executors.newCachedThreadPool();;
      System.out.println("size of mypool:" + mypool.getPoolSize());
      excr.submit(new Threadimpl());
      excr.submit(new Threadimpl());
      System.out.println("Total number threads scheduled):"+ mypool.getTaskCount());
      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();
         }
      }
   }
}

輸出:

size of mypool:0
Thread Name:ForkJoinPool-1-worker-5
after sleep Thread Name:ForkJoinPool-1-worker-5
Thread Name:ForkJoinPool-1-worker-3
after sleep Thread Name:ForkJoinPool-1-worker-3
Total number threads scheduled):0

例子1

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

public class ExecutorsnewWorkStealingPoolExample2  {
	
   public static void main(final String[] arguments) throws InterruptedException {
      ExecutorService excr = Executors.newWorkStealingPool(4);
      ThreadPoolExecutor mypool = (ThreadPoolExecutor)  Executors.newCachedThreadPool();;
      System.out.println("size of mypool:" + mypool.getPoolSize());
      excr.submit(new Threadimpl());
      excr.submit(new Threadimpl());
      System.out.println("Total number threads scheduled):"+ mypool.getTaskCount());
      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();
         }
      }
   }
}

輸出:

size of mypool:0
Thread Name:ForkJoinPool-1-worker-5
after sleep Thread Name:ForkJoinPool-1-worker-5
Thread Name:ForkJoinPool-1-worker-3
after sleep Thread Name:ForkJoinPool-1-worker-3
Total number threads scheduled):0




相關用法


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