Skip to content

Commit dcaa2c8

Browse files
committed
20241218 동시성 병렬성 연
1 parent 8d94d38 commit dcaa2c8

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5-
5+
/*
6+
* 동시성 - CPU 개수 < 작업의 개수에 최적화 되어있습니다.
7+
*
8+
* 만약 CPU 개수보다 작업의 개수가 한개라도 많다면 동시성 처리로 진행된다.
9+
* */
610
public class ConcurrencyExample {
711
public static void main(String[] args) {
8-
12+
System.out.println("CPU 개수 : "+Runtime.getRuntime().availableProcessors());
13+
// CPU 개수 * 2
914
// int cpuCores = Runtime.getRuntime().availableProcessors() * 2;
15+
16+
// CPU 개수 + 1
1017
int cpuCores = 13;
1118

1219
// CPU 개수를 초과하는 데이터를 생성

src/main/java/io/concurrency/chapter01/exam01/ParallelismExample.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5-
5+
/*
6+
* 병렬성 - CPU 개수 >= 작업의 개수에 최적화 되어있습니다.
7+
* */
68
public class ParallelismExample {
79
public static void main(String[] args) {
8-
// int cpuCores = 1;
10+
System.out.println("CPU 개수 : "+Runtime.getRuntime().availableProcessors());
11+
912
int cpuCores = Runtime.getRuntime().availableProcessors();
1013

1114
// CPU 개수만큼 데이터를 생성
@@ -16,7 +19,15 @@ public static void main(String[] args) {
1619

1720
// CPU 개수만큼 데이터를 병렬로 처리
1821
long startTime1 = System.currentTimeMillis();
19-
long sum1 = data.parallelStream()
22+
23+
/*
24+
* 병렬스트림 진행 (CPU를 전부 사용해서 각각 작업을 하나씩 물기)
25+
* */
26+
// long sum1 = data.parallelStream()
27+
/*
28+
* 일반스트림 진행 (CPU를 하나만 사용해서 작업을 진행)
29+
* */
30+
long sum1 = data.stream()
2031
.mapToLong(i -> {
2132
try {
2233
Thread.sleep(500);

0 commit comments

Comments
 (0)