[java] 자바 배치 프로세싱에서의 병렬 처리를 위한 스레드 제어 방법은 무엇인가?

아래는 병렬 처리를 위한 스레드 제어 방법에 대한 예시 코드입니다.

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

public class ParallelBatchProcessing {
    public static void main(String[] args) {
        // 스레드 풀 생성
        ExecutorService executor = Executors.newFixedThreadPool(5);

        for (int i = 0; i < 10; i++) {
            final int taskNumber = i;
            // 작업을 스레드에 제출
            executor.submit(() -> processTask(taskNumber));
        }

        // 작업이 모두 완료될 때까지 대기
        executor.shutdown();
        while (!executor.isTerminated()) {
            // 대기
        }
        System.out.println("모든 작업 완료");
    }

    private static void processTask(int taskNumber) {
        System.out.println("작업 " + taskNumber + " 처리 중");
        // 작업 처리
    }
}

위 코드에서 ExecutorService를 사용하여 스레드 풀을 생성하고, submit 메서드를 통해 작업을 제출합니다. 작업이 모두 완료될 때까지 대기한 후 스레드 풀을 종료합니다.

참고 문헌: