스트림 병렬 처리 이해

import java.util.Arrays;
import java.util.List;

public class ParallelReduceMinMax {

	public static void main(final String[] args) {
		final List<Integer> intList = Arrays.asList(4, 2, 8, 1, 9, 6, 7, 3, 5);

		// 최대값 구하기
		final Integer max = intList.parallelStream().reduce(1, Integer::max);
		System.out.println("최대값 = " + max);

		// 최소값 구하기
		final Integer min = intList.parallelStream().reduce(1, Integer::min);
		System.out.println("최소값 = " + min);
	}
}
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 병렬 스트림 API 사용 예
 */
public class InsideParallelStream {

	public static void main(final String[] args) {
		final List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

		final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

		// 스트림 내부의 스레드 값을 구함
		intList.parallelStream().forEach(value -> {
			// 현재 스레드 이름을 구한다.
			final String threadName = Thread.currentThread().getName();

			// 스레드 이름과 데이터 값을 출력한다.
			final LocalDateTime currentTime = LocalDateTime.now();
			System.out.printf(currentTime.format(formatter) + " -> Thread Name : %s, Stream Value : %s\\n", threadName, value);

			// 시간 확인을 위해 2초간 sleep한다.
			try {
				TimeUnit.SECONDS.sleep(2);
			} catch (final InterruptedException ignore) {
			}
		});
	}
}
2022-07-02 22:48:30 -> Thread Name : ForkJoinPool.commonPool-worker-27, Stream Value : 5
2022-07-02 22:48:30 -> Thread Name : ForkJoinPool.commonPool-worker-5, Stream Value : 9
2022-07-02 22:48:30 -> Thread Name : ForkJoinPool.commonPool-worker-3, Stream Value : 4
2022-07-02 22:48:30 -> Thread Name : ForkJoinPool.commonPool-worker-31, Stream Value : 8
2022-07-02 22:48:30 -> Thread Name : ForkJoinPool.commonPool-worker-9, Stream Value : 1
2022-07-02 22:48:30 -> Thread Name : ForkJoinPool.commonPool-worker-19, Stream Value : 3
2022-07-02 22:48:30 -> Thread Name : ForkJoinPool.commonPool-worker-17, Stream Value : 6
2022-07-02 22:48:30 -> Thread Name : ForkJoinPool.commonPool-worker-13, Stream Value : 10
2022-07-02 22:48:30 -> Thread Name : main, Stream Value : 7
2022-07-02 22:48:30 -> Thread Name : ForkJoinPool.commonPool-worker-23, Stream Value : 2

병렬처리의 특징

스레드 개수 제어

ForkJoinPool의 기본 스레드 값을 변경한다.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

/**
 * 병렬 스트림 API 사용 예
 */
public class InsideParallelStream2 {

	public static void main(final String[] args) {
		final List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

		final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

		// 스레드 개수 2개로 설정
		System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "2");
		System.out.printf("## Thread Pool Size: %s\\n", ForkJoinPool.getCommonPoolParallelism());

		// 스트림 내부의 스레드 값을 구함
		intList.parallelStream().forEach(value -> {
			// 현재 스레드 이름을 구한다.
			final String threadName = Thread.currentThread().getName();

			// 스레드 이름과 데이터 값을 출력한다.
			final LocalDateTime currentTime = LocalDateTime.now();
			System.out.printf(currentTime.format(formatter) + " -> Thread Name : %s, Stream Value : %s\\n", threadName, value);

			// 시간 확인을 위해 2초간 sleep한다.
			try {
				TimeUnit.SECONDS.sleep(2);
			} catch (final InterruptedException ignore) {
			}
		});
	}
}
## Thread Pool Size: 2
2022-07-02 22:56:59 -> Thread Name : main, Stream Value : 7
2022-07-02 22:56:59 -> Thread Name : ForkJoinPool.commonPool-worker-3, Stream Value : 3
2022-07-02 22:56:59 -> Thread Name : ForkJoinPool.commonPool-worker-1, Stream Value : 9
2022-07-02 22:57:01 -> Thread Name : main, Stream Value : 6
2022-07-02 22:57:01 -> Thread Name : ForkJoinPool.commonPool-worker-1, Stream Value : 10
2022-07-02 22:57:01 -> Thread Name : ForkJoinPool.commonPool-worker-3, Stream Value : 5
2022-07-02 22:57:03 -> Thread Name : ForkJoinPool.commonPool-worker-3, Stream Value : 4
2022-07-02 22:57:03 -> Thread Name : ForkJoinPool.commonPool-worker-1, Stream Value : 8
2022-07-02 22:57:05 -> Thread Name : ForkJoinPool.commonPool-worker-3, Stream Value : 2
2022-07-02 22:57:05 -> Thread Name : ForkJoinPool.commonPool-worker-1, Stream Value : 1

ForkJoinPool이 아닌 다른 스레드 풀을 사용한다.