Skip to content

Commit 904e197

Browse files
committed
lecture
1 parent eb13603 commit 904e197

File tree

7 files changed

+122
-130
lines changed

7 files changed

+122
-130
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.concurrency.chapter11.exam12;
2+
3+
import java.util.concurrent.ForkJoinPool;
4+
5+
public class CustomForkJoinPoolExample {
6+
7+
public static void main(String[] args) {
8+
9+
int[] array = new int[10];
10+
for (int i = 0; i < array.length; i++) {
11+
array[i] = i;
12+
}
13+
ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
14+
15+
CustomRecursiveTask task = new CustomRecursiveTask(array, 0, array.length);
16+
long result = pool.invoke(task);
17+
18+
System.out.println("총 합계 " + result);
19+
System.out.println("풀의 수행 정보 " + pool);
20+
}
21+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.concurrency.chapter11.exam12;
2+
3+
import java.util.concurrent.RecursiveTask;
4+
5+
public class CustomRecursiveTask extends RecursiveTask<Long> {
6+
private static final int THRESHOLD = 2;
7+
private final int[] array;
8+
private final int start;
9+
private final int end;
10+
11+
public CustomRecursiveTask(int[] array, int start, int end) {
12+
this.array = array;
13+
this.start = start;
14+
this.end = end;
15+
}
16+
17+
@Override
18+
protected Long compute() {
19+
if (end - start < THRESHOLD) {
20+
long sum = 0;
21+
for (int i = start; i < end; i++) {
22+
sum += array[i];
23+
}
24+
25+
return sum;
26+
} else {
27+
int mid = start + (end - start) / 2;
28+
CustomRecursiveTask left = new CustomRecursiveTask(array, start, mid);
29+
CustomRecursiveTask right = new CustomRecursiveTask(array, mid, end);
30+
31+
left.fork();
32+
long rightResult = right.compute();
33+
long leftResult = left.join();
34+
35+
return leftResult + rightResult;
36+
}
37+
}
38+
}

src/main/java/io/concurrency/chapter11/exam12/ForkJoinPoolCommonPool.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/java/io/concurrency/chapter11/exam12/ForkJoinPoolCustomPool.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/main/java/io/concurrency/chapter11/exam12/MixedThreadPoolChained.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.concurrency.chapter11.exam12;
2+
3+
import java.util.stream.IntStream;
4+
5+
public class ParallelStreamExample{
6+
public static void main(String[] args) {
7+
8+
int[] array = IntStream.range(0, 10).toArray();
9+
long sum = IntStream.of(array).parallel().sum();
10+
11+
System.out.println("The sum is " + sum);
12+
}
13+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.concurrency.chapter11.exam12;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.ForkJoinPool;
7+
8+
public class ThreadPoolChainedExample {
9+
10+
public static void main(String[] args) throws InterruptedException {
11+
12+
ExecutorService executor = Executors.newFixedThreadPool(2);
13+
ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors()-1);
14+
15+
CompletableFuture.supplyAsync(() -> {
16+
System.out.println("IO Bound 작업: " + Thread.currentThread().getName());
17+
return 1;
18+
}, executor)
19+
20+
.thenApplyAsync(res -> {
21+
System.out.println("CPU Bound 작업: " + Thread.currentThread().getName());
22+
return res + 1;
23+
})
24+
25+
.thenApplyAsync(res -> {
26+
System.out.println("IO Bound 작업: " + Thread.currentThread().getName());
27+
return res + 2;
28+
29+
},executor)
30+
31+
.thenApplyAsync(res -> {
32+
System.out.println("CPU Bound 작업: " + Thread.currentThread().getName());
33+
return res + 3;
34+
})
35+
36+
.thenComposeAsync(res -> {
37+
System.out.println("병렬 처리: " + Thread.currentThread().getName());
38+
return CompletableFuture.supplyAsync(() -> res + 4);
39+
40+
})
41+
42+
.thenAcceptAsync(res -> {
43+
System.out.println("병렬 처리: " + Thread.currentThread().getName());
44+
System.out.println("최종 결과 : " + res);
45+
}).join();
46+
47+
executor.shutdown();
48+
forkJoinPool.shutdown();
49+
}
50+
}

0 commit comments

Comments
 (0)