Java Tips

Ads

感想

java.util.concurrency パッケージが出来て、マルチスレッドプログラムがとてもやりやすくなったっぽいので Executor を使ったサンプルを書いてみた。
concurrency には他にも有用なクラスがあるが、基本的には Executor を抑えておけばよいと思う。

テストコード

ソース

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

import junit.framework.TestCase;

public class ExecutorSimpleTest extends TestCase {

	/**
	 * ExecuterService の切り替えだけで簡単にマルチスレッドが使えることを試してみるサンプル。
	 * @throws InterruptedException
	 */
	public void testSomeExecuter() throws InterruptedException {
		log("シングルスレッドで実行");
		log("ExecutorService = Executors.newSingleThreadExecutor()");
		submitManyRunnables(Executors.newSingleThreadExecutor());
		log("固定数のスレッドで実行");
		log("ExecutorService = Executors.newFixedThreadPool(3)");
		submitManyRunnables(Executors.newFixedThreadPool(3));
		log("動的に必要なだけスレッド生成して実行");
		log("ExecutorService = Executors.newCachedThreadPool()");
		submitManyRunnables(Executors.newCachedThreadPool());
	}
	
	private void submitManyRunnables(ExecutorService executer) throws InterruptedException {
		//10個のRunnableを投入
		for(int i = 0; i < 10; i++) {
			final int j = i;
			executer.submit(new Runnable(){
				public void run() {
					log("job(" + j + ") start, thread=" + Thread.currentThread().getName());
					try {
						TimeUnit.SECONDS.sleep(1);
					} catch (InterruptedException e) {
						log("job(" + j + ") Interrupted, thread=" + Thread.currentThread().getName());
					}
					log("job(" + j + ") end,   thread=" + Thread.currentThread().getName());
				}
			});
		}
		log("all submitted");
		
		TimeUnit.SECONDS.sleep(5);
		log("shutdownNow() //submitから5秒待って停止命令も試してみる");
		executer.shutdownNow();
		log("shutdownNow() invoked");

		//適当に間合わせ
		TimeUnit.SECONDS.sleep(2);
		System.out.println();
	}
	
	private void log(String str) {
		System.out.println(new Date() + ": " + str);
	}
}

実行結果

Tue Apr 19 18:40:57 JST 2005: シングルスレッドで実行
Tue Apr 19 18:40:57 JST 2005: ExecutorService = Executors.newSingleThreadExecutor()
Tue Apr 19 18:40:57 JST 2005: all submitted
Tue Apr 19 18:40:57 JST 2005: job(0) start, thread=pool-1-thread-1
Tue Apr 19 18:40:58 JST 2005: job(0) end,   thread=pool-1-thread-1
Tue Apr 19 18:40:58 JST 2005: job(1) start, thread=pool-1-thread-1
Tue Apr 19 18:40:59 JST 2005: job(1) end,   thread=pool-1-thread-1
Tue Apr 19 18:40:59 JST 2005: job(2) start, thread=pool-1-thread-1
Tue Apr 19 18:41:00 JST 2005: job(2) end,   thread=pool-1-thread-1
Tue Apr 19 18:41:00 JST 2005: job(3) start, thread=pool-1-thread-1
Tue Apr 19 18:41:01 JST 2005: job(3) end,   thread=pool-1-thread-1
Tue Apr 19 18:41:01 JST 2005: job(4) start, thread=pool-1-thread-1
Tue Apr 19 18:41:02 JST 2005: shutdownNow() //submitから5秒待って停止命令も試してみる
Tue Apr 19 18:41:02 JST 2005: shutdownNow() invoked
Tue Apr 19 18:41:02 JST 2005: job(4) Interrupted, thread=pool-1-thread-1
Tue Apr 19 18:41:02 JST 2005: job(4) end,   thread=pool-1-thread-1

Tue Apr 19 18:41:04 JST 2005: 固定数のスレッドで実行
Tue Apr 19 18:41:04 JST 2005: ExecutorService = Executors.newFixedThreadPool(3)
Tue Apr 19 18:41:04 JST 2005: all submitted
Tue Apr 19 18:41:04 JST 2005: job(0) start, thread=pool-2-thread-1
Tue Apr 19 18:41:04 JST 2005: job(1) start, thread=pool-2-thread-2
Tue Apr 19 18:41:04 JST 2005: job(2) start, thread=pool-2-thread-3
Tue Apr 19 18:41:05 JST 2005: job(0) end,   thread=pool-2-thread-1
Tue Apr 19 18:41:05 JST 2005: job(3) start, thread=pool-2-thread-1
Tue Apr 19 18:41:05 JST 2005: job(1) end,   thread=pool-2-thread-2
Tue Apr 19 18:41:05 JST 2005: job(4) start, thread=pool-2-thread-2
Tue Apr 19 18:41:05 JST 2005: job(2) end,   thread=pool-2-thread-3
Tue Apr 19 18:41:05 JST 2005: job(5) start, thread=pool-2-thread-3
Tue Apr 19 18:41:06 JST 2005: job(3) end,   thread=pool-2-thread-1
Tue Apr 19 18:41:06 JST 2005: job(6) start, thread=pool-2-thread-1
Tue Apr 19 18:41:06 JST 2005: job(4) end,   thread=pool-2-thread-2
Tue Apr 19 18:41:06 JST 2005: job(5) end,   thread=pool-2-thread-3
Tue Apr 19 18:41:06 JST 2005: job(8) start, thread=pool-2-thread-3
Tue Apr 19 18:41:06 JST 2005: job(7) start, thread=pool-2-thread-2
Tue Apr 19 18:41:07 JST 2005: job(6) end,   thread=pool-2-thread-1
Tue Apr 19 18:41:07 JST 2005: job(9) start, thread=pool-2-thread-1
Tue Apr 19 18:41:07 JST 2005: job(8) end,   thread=pool-2-thread-3
Tue Apr 19 18:41:07 JST 2005: job(7) end,   thread=pool-2-thread-2
Tue Apr 19 18:41:08 JST 2005: job(9) end,   thread=pool-2-thread-1
Tue Apr 19 18:41:09 JST 2005: shutdownNow() //submitから5秒待って停止命令も試してみる
Tue Apr 19 18:41:09 JST 2005: shutdownNow() invoked

Tue Apr 19 18:41:11 JST 2005: 動的に必要なだけスレッド生成して実行
Tue Apr 19 18:41:11 JST 2005: ExecutorService = Executors.newCachedThreadPool()
Tue Apr 19 18:41:11 JST 2005: job(0) start, thread=pool-3-thread-1
Tue Apr 19 18:41:11 JST 2005: job(1) start, thread=pool-3-thread-2
Tue Apr 19 18:41:11 JST 2005: job(2) start, thread=pool-3-thread-3
Tue Apr 19 18:41:11 JST 2005: job(3) start, thread=pool-3-thread-4
Tue Apr 19 18:41:11 JST 2005: job(4) start, thread=pool-3-thread-5
Tue Apr 19 18:41:11 JST 2005: job(5) start, thread=pool-3-thread-6
Tue Apr 19 18:41:11 JST 2005: job(6) start, thread=pool-3-thread-7
Tue Apr 19 18:41:11 JST 2005: all submitted
Tue Apr 19 18:41:11 JST 2005: job(7) start, thread=pool-3-thread-8
Tue Apr 19 18:41:11 JST 2005: job(8) start, thread=pool-3-thread-9
Tue Apr 19 18:41:11 JST 2005: job(9) start, thread=pool-3-thread-10
Tue Apr 19 18:41:12 JST 2005: job(0) end,   thread=pool-3-thread-1
Tue Apr 19 18:41:12 JST 2005: job(1) end,   thread=pool-3-thread-2
Tue Apr 19 18:41:12 JST 2005: job(2) end,   thread=pool-3-thread-3
Tue Apr 19 18:41:12 JST 2005: job(3) end,   thread=pool-3-thread-4
Tue Apr 19 18:41:12 JST 2005: job(4) end,   thread=pool-3-thread-5
Tue Apr 19 18:41:12 JST 2005: job(5) end,   thread=pool-3-thread-6
Tue Apr 19 18:41:12 JST 2005: job(6) end,   thread=pool-3-thread-7
Tue Apr 19 18:41:12 JST 2005: job(7) end,   thread=pool-3-thread-8
Tue Apr 19 18:41:12 JST 2005: job(9) end,   thread=pool-3-thread-10
Tue Apr 19 18:41:12 JST 2005: job(8) end,   thread=pool-3-thread-9
Tue Apr 19 18:41:16 JST 2005: shutdownNow() //submitから5秒待って停止命令も試してみる
Tue Apr 19 18:41:16 JST 2005: shutdownNow() invoked

コメント



トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS