0
0

Java Gold 例題 並列処理

Last updated at Posted at 2024-08-21
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CachedThreadPoolTest {
	
	private static final ExecutorService threadPool = Executors./* insert code */
 
	public static void main(String[] args) {
		startServer();
	}

	public static void startServer() {
		for(int i = 0; i < 6; i++) {
			final int requestNumber = i;
			threadPool.submit(() -> {
				handleRequest(requestNumber);
			});
		}
		threadPool.shutdown();
	}
	
	public static void handleRequest(int requestNumber) {
		System.out.println("request number : " + requestNumber);
		System.out.println("thread : " + Thread.currentThread().getName());
		
		try {
			Thread.sleep(1000);
		} catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}

/* insert code */部分にExecutorsのファクトリメソッドを挿入して次の実行結果が得られました。

1回目の実行結果

request number : 2
request number : 0
request number : 5
request number : 3
request number : 4
request number : 1
thread : pool-1-thread-3
thread : pool-1-thread-2
thread : pool-1-thread-6
thread : pool-1-thread-5
thread : pool-1-thread-1
thread : pool-1-thread-4

1回目の実行後/* insert code */部分を修正して、別のExecutorsのファクトリメソッドを挿入して次の実行結果が得られました。

2回目の実行結果

request number : 1
request number : 0
thread : pool-1-thread-2
thread : pool-1-thread-1
request number : 2
request number : 3
thread : pool-1-thread-1
thread : pool-1-thread-2
request number : 4
thread : pool-1-thread-1
request number : 5
thread : pool-1-thread-2

/* insert code */にあてはまるコードについて
 ・1回目の実行結果にあてはまるファクトリメソッドを1つ選んでください。
 ・2回目の実行結果にあてはまるファクトリメソッドを2つ選んでください。

  1. newThreadPool();
  2. newThreadPool(2);
  3. newSingleThreadExecutor();
  4. newFixedThreadPool();
  5. newFixedThreadPool(2);
  6. newCachedThreadPool();
  7. newCachedThreadPool(2);
  8. newScheduledThreadPool();
  9. newScheduledThreadPool(2);
  10. scheduleAtFixedRate();
  11. scheduleWithFixedDelay();
解答

正解
1回目: 6
2回目: 5, 9

1,2
newThreadPool();
newThreadPool(2);
存在しません。

3

newSingleThreadExecutor()
一つのスレッドを生成します。

実行結果は以下になります。

request number : 0
thread : pool-1-thread-1
request number : 1
thread : pool-1-thread-1
request number : 2
thread : pool-1-thread-1
request number : 3
thread : pool-1-thread-1
request number : 4
thread : pool-1-thread-1
request number : 5
thread : pool-1-thread-1

4
引数なしのnewFixedThreadPool();は存在しません。

5
2回目の実行結果にたいしてありうるファクトリメソッドです。
newFixedThreadPool(int nThreads)

最初の2つのタスクが送信されたとき、2つのスレッドがそれぞれ1つずつタスクを処理するため、実行結果が被ります。
その後、次のタスクは空いているスレッドによって処理されます。
スレッドがタスクを処理し終わった時点で、新しいタスクがそのスレッドに割り当てられます。
タスクの実行順序はスレッドの空き具合によって決まります。

6
1回目の実行結果にたいしてありうるファクトリメソッドです。
newCachedThreadPool()

7
引数にintを指定するnewCachedThreadPool(int num)メソッドは存在しません。

ただし、引数にThreadFactoryを指定するnewCachedThreadPool(ThreadFactory threadFactory)メソッドは存在します。

newCachedThreadPool​(ThreadFactory threadFactory)
必要に応じ、新規スレッドを作成するスレッド・プールを作成しますが、利用可能な場合には以前に構築されたスレッドを再利用します。

8
引数なしのnewScheduledThreadPool​()は存在しません。

9
2回目の実行結果にたいしてありうるファクトリメソッドです。
newScheduledThreadPool​(int corePoolSize)

newFixedThreadPool(2)は単純なタスクキューとスレッド管理を行いますが、newScheduledThreadPool(2)はより複雑なタスクスケジュール管理を行います。

10, 11
ScheduledExecutorServiceインターフェイスのメソッドです。ファクトリメソッドではありません

scheduleAtFixedRate()
scheduleWithFixedDelay()

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0