【Java】【非同期】 ThreadPoolExecutor ~スレッドプール ~ [1]

ThreadPoolExecutor

 * プールされているスレッドと処理すべきタスクのリストを管理
 * 以下の関連記事で扱ったWorker Thread パターン(別名スレッドプールパターン)を実装するのに使える
http://blogs.yahoo.co.jp/dk521123/32918314.html
# ThreadPoolExecutorが、ChannelとWorker を兼ね備えている

スレッドプールの種類

[1] newFixedThreadPool
 => 指定したスレッド数を使いまわして、タスクを処理する。

[2] newCachedThreadPool
 => 複数スレッドでタスクを処理。

スレッドプールのインスタンス作成方法

[1] new ThreadPoolExecutor(引数)で作成
 => 以下の関連記事を参照のこと。
http://blogs.yahoo.co.jp/dk521123/33942167.html
[2] ファクトリーメソッド(例「executor = Executors.newCachedThreadPool()」)で作成
 => 以下のサンプルを参照

サンプル

http://blogs.yahoo.co.jp/dk521123/32918314.html
とほぼ同じことをやってみる

IRequest.java

public interface IRequest extends Runnable {
  // 何もない
}

Request.java

import java.util.Random;

public class Request implements IRequest {
  // 依頼者
  private final String companyName;
  // リクエストの番号
  private final int requestNumber;

  public SampleRequest(String companyName, int requestNumber) {
    this.companyName = companyName;
    this.requestNumber = requestNumber;
  }

  @Override
  public void run() {
    System.out.println(Thread.currentThread().getName() + " executes " + this);
    try {
      this.doHeavyJob();
    } catch (InterruptedException e) {
    }
  }

  public String toString() {
    return "[ Request from " + this.companyName + " No." + this.requestNumber + " ]";
  }

  private void doHeavyJob() throws InterruptedException {
    Random random = new Random();
    Thread.sleep(random.nextInt(1000));
  }
}

SampleWorkerThread.java

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

public class SampleWorkerThread {
  private ExecutorService executor;
  
  public SampleWorkerThread() {
    this.executor = Executors.newCachedThreadPool();
  }
  
  public void putRequest(IRequest request) {
    this.executor.execute(request);
  }
  
  public void quit() {
    this.executor.shutdown();
  }
}

Main.java

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {

  public static void main(String[] args) throws InterruptedException {
    SampleWorkerThread sampleWorkerThread = new SampleWorkerThread();

    List<String> companyNames = new ArrayList<String>() {
      private static final long serialVersionUID = 1L;
      {
        add("Yahoo");
        add("Google");
        add("Oracle");
      }
    };

    for (int i = 0; i < 100; i++) {
      String companyName = companyNames.get(i % 3);
      sampleWorkerThread.putRequest(new Request(companyName, i));
      doHeavyJob();
    }

    sampleWorkerThread.quit();
  }

  private static void doHeavyJob() throws InterruptedException {
    Random random = new Random();
    Thread.sleep(random.nextInt(10000));
  }
}

出力結果

[Start] pool-1-thread-1 executes [ Request from Yahoo No.0 ]
[Start] pool-1-thread-2 executes [ Request from Google No.1 ]
[End] pool-1-thread-2 executes [ Request from Google No.1 ]
[Start] pool-1-thread-2 executes [ Request from Oracle No.2 ]
[Start] pool-1-thread-3 executes [ Request from Yahoo No.3 ]
[Start] pool-1-thread-4 executes [ Request from Google No.4 ]
[Start] pool-1-thread-5 executes [ Request from Oracle No.5 ]
[Start] pool-1-thread-6 executes [ Request from Yahoo No.6 ]
[Start] pool-1-thread-7 executes [ Request from Google No.7 ]
[End] pool-1-thread-3 executes [ Request from Yahoo No.3 ]
[Start] pool-1-thread-3 executes [ Request from Oracle No.8 ]
[Start] pool-1-thread-8 executes [ Request from Yahoo No.9 ]
[End] pool-1-thread-7 executes [ Request from Google No.7 ]
[Start] pool-1-thread-7 executes [ Request from Google No.10 ]
[End] pool-1-thread-6 executes [ Request from Yahoo No.6 ]
[Start] pool-1-thread-6 executes [ Request from Oracle No.11 ]
[End] pool-1-thread-6 executes [ Request from Oracle No.11 ]
[Start] pool-1-thread-6 executes [ Request from Yahoo No.12 ]
[Start] pool-1-thread-9 executes [ Request from Google No.13 ]
[End] pool-1-thread-1 executes [ Request from Yahoo No.0 ]
[Start] pool-1-thread-1 executes [ Request from Oracle No.14 ]
[Start] pool-1-thread-10 executes [ Request from Yahoo No.15 ]
[End] pool-1-thread-8 executes [ Request from Yahoo No.9 ]
[End] pool-1-thread-2 executes [ Request from Oracle No.2 ]
[Start] pool-1-thread-2 executes [ Request from Google No.16 ]
...


関連記事

デザインパターン】【非同期】Worker Thread パターン

http://blogs.yahoo.co.jp/dk521123/32918314.html

ThreadPoolExecutor ~スレッドプール ~ [2]

http://blogs.yahoo.co.jp/dk521123/33942167.html