-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
243p 상단 어떻게 동기화해야할까? 이해가 안가서 생각해보았습니다. 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);
}
} |
Q) 244p, 어떤 연산을 병렬로 실행하고 어떤 연산을 순차로 실행할지 제어할 수 있다 라고 되어있는데, 마지막에는 또 파이프라인이 마지막 호출은 parallel 이므로 파이프라인은 전체적으로 병렬로 실행된다고 말한다. 무슨 말인지 모르겠음. stream.parallel()
.filter(...)
.sequenctial()
.map(...)
.parallel()
.reduce() |
Q) 249p. 결과적으로 병렬 버전이 쿼드 코어 CPU 활용을 못하고 순차 버전에 비해 다섯배나 느리다.
리듀싱 과정을 시작하는 시점에 전체 숫자 리스트가 준비되지 않았으므로 스트림을 병렬로 처리할 수 있도록 청크로 분할 할 수 없다.?? 어렵네요..왜 병렬 처리가 안되는지 이해가 잘 안갑니다. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CHAPTER 7 병렬 데이터 처리와 성능
7.1 병렬 스트림 @kyupid
7.2 포크/조인 프레임워크 @asas6978
7.3 Spliterator 인터페이스 @Soojae
7.4 마치며
The text was updated successfully, but these errors were encountered: