Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHAPTER 7 병렬 데이터 처리와 성능 #12

Open
kyupid opened this issue Apr 15, 2023 · 3 comments
Open

CHAPTER 7 병렬 데이터 처리와 성능 #12

kyupid opened this issue Apr 15, 2023 · 3 comments
Assignees

Comments

@kyupid
Copy link
Member

kyupid commented Apr 15, 2023

CHAPTER 7 병렬 데이터 처리와 성능

7.1 병렬 스트림 @kyupid
7.2 포크/조인 프레임워크 @asas6978
7.3 Spliterator 인터페이스 @Soojae
7.4 마치며

@kyupid
Copy link
Member Author

kyupid commented Apr 19, 2023

243p 상단 어떻게 동기화해야할까? 이해가 안가서 생각해보았습니다.

https://github.com/java-piledrivers/modern-java-in-action/tree/main/Chapter%2001%20-%20%EC%9E%90%EB%B0%94%208%2C%209%2C%2010%2C%2011%20%EB%AC%B4%EC%8A%A8%20%EC%9D%BC%EC%9D%B4%20%EC%9D%BC%EC%96%B4%EB%82%98%EA%B3%A0%20%EC%9E%88%EB%8A%94%EA%B0%80/1.2

public class ParallelArraySum {
    private int[] array;
    private int numOfThreads;

    public ParallelArraySum(int[] array, int numOfThreads) {
        this.array = array;
        this.numOfThreads = numOfThreads;
    }

    public int sum() throws InterruptedException {
        int size = (int) Math.ceil(array.length * 1.0 / numOfThreads);
        int[] sums = new int[numOfThreads];
        Thread[] threads = new Thread[numOfThreads];

        // 아래 for 문을 동기화라고 생각하면 될것같다. 동기화란 레이스 컨디션이 일어나지 않도록 하는 것이다.
        // 그 의미가 통하는 통하는 것이 아래 코드이다.
        // 각 쓰레드들은 자신이 수행해야할 청크를 지정하고 있다. (start 부터 end)
        // 즉, 쓰레드마다 스트림의 어느 청크를 수행할것인지 동기화를 해주는 것이다.
        for (int i = 0; i < numOfThreads; i++) { 
            final int start = i * size;
            final int end = (i + 1) * size;
            threads[i] = new Thread(() -> {
                int sum = 0;
                for (int j = start; j < end && j < array.length; j++) {
                    sum += array[j];
                }
                sums[i] = sum;
            });
            threads[i].start();
        }

        for (int i = 0; i < numOfThreads; i++) {
            threads[i].join();
        }

        int sum = 0;
        for (int i = 0; i < numOfThreads; i++) {
            sum += sums[i];
        }

        return sum;
    }

    public static void main(String[] args) throws InterruptedException {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        ParallelArraySum parallelArraySum = new ParallelArraySum(array, 4);
        int sum = parallelArraySum.sum();
        System.out.println("Sum: " + sum);
    }
}

@kyupid
Copy link
Member Author

kyupid commented Apr 20, 2023

Q) 244p, 어떤 연산을 병렬로 실행하고 어떤 연산을 순차로 실행할지 제어할 수 있다 라고 되어있는데, 마지막에는 또 파이프라인이 마지막 호출은 parallel 이므로 파이프라인은 전체적으로 병렬로 실행된다고 말한다. 무슨 말인지 모르겠음.

stream.parallel()
      .filter(...)
      .sequenctial()
      .map(...)
      .parallel()
      .reduce()

@kyupid
Copy link
Member Author

kyupid commented Apr 20, 2023

Q) 249p.

결과적으로 병렬 버전이 쿼드 코어 CPU 활용을 못하고 순차 버전에 비해 다섯배나 느리다.
하지만 두가지 문제점이 있었다.

  • 반복 결과로 박싱도니 객체가 만들어지므로 숫자를 더하려면 언박싱을 해야 한다.
  • 반복 작업은 병렬로 수행할 수 있는 독립 단위로 나누기가 어렵다.

리듀싱 과정을 시작하는 시점에 전체 숫자 리스트가 준비되지 않았으므로 스트림을 병렬로 처리할 수 있도록 청크로 분할 할 수 없다.??

어렵네요..왜 병렬 처리가 안되는지 이해가 잘 안갑니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants