From d43df41a62cf508e3a0c0c516bb931883acfed28 Mon Sep 17 00:00:00 2001 From: LimZida Date: Wed, 18 Dec 2024 20:05:52 +0900 Subject: [PATCH 1/7] 20241218 test --- .../io/concurrency/chapter01/exam01/ConcurrencyExample.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java b/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java index dcae22a..0463d46 100644 --- a/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java +++ b/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java @@ -6,8 +6,8 @@ public class ConcurrencyExample { public static void main(String[] args) { - int cpuCores = Runtime.getRuntime().availableProcessors() * 2; -// int cpuCores = 13; +// int cpuCores = Runtime.getRuntime().availableProcessors() * 2; + int cpuCores = 13; // CPU 개수를 초과하는 데이터를 생성 List data = new ArrayList<>(); From 5bffcbc18cef687b65c506a25a52249b6840edc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=ED=98=84=EC=98=81?= <87633455+LimZida@users.noreply.github.com> Date: Wed, 18 Dec 2024 20:08:25 +0900 Subject: [PATCH 2/7] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ba4acf1..cb1304b 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ # Java-Concurrency-Programming +장수원님의 자바 동시성 프로그래밍 [리액티브 프로그래밍 Part.1] 에 대한 스터디 목적의 Repo입니다. +모든 코드의 저작권은 https://github.com/onjsdnjs/Java-Concurrency-Programming에 존재합니다. From 8d94d38c242b202432c2f0ad32ef92da79b0328b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=ED=98=84=EC=98=81?= <87633455+LimZida@users.noreply.github.com> Date: Wed, 18 Dec 2024 20:08:53 +0900 Subject: [PATCH 3/7] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cb1304b..9714420 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # Java-Concurrency-Programming 장수원님의 자바 동시성 프로그래밍 [리액티브 프로그래밍 Part.1] 에 대한 스터디 목적의 Repo입니다. -모든 코드의 저작권은 https://github.com/onjsdnjs/Java-Concurrency-Programming에 존재합니다. + +모든 코드의 저작권은 https://github.com/onjsdnjs/Java-Concurrency-Programming 에 존재합니다. From dcaa2c8b51049570d4cd23c9ad85cd35ba80b201 Mon Sep 17 00:00:00 2001 From: LimZida Date: Wed, 18 Dec 2024 21:08:09 +0900 Subject: [PATCH 4/7] =?UTF-8?q?20241218=20=EB=8F=99=EC=8B=9C=EC=84=B1=20?= =?UTF-8?q?=EB=B3=91=EB=A0=AC=EC=84=B1=20=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chapter01/exam01/ConcurrencyExample.java | 11 +++++++++-- .../chapter01/exam01/ParallelismExample.java | 17 ++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java b/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java index 0463d46..7a4d3a1 100644 --- a/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java +++ b/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java @@ -2,11 +2,18 @@ import java.util.ArrayList; import java.util.List; - +/* +* 동시성 - CPU 개수 < 작업의 개수에 최적화 되어있습니다. +* +* 만약 CPU 개수보다 작업의 개수가 한개라도 많다면 동시성 처리로 진행된다. +* */ public class ConcurrencyExample { public static void main(String[] args) { - + System.out.println("CPU 개수 : "+Runtime.getRuntime().availableProcessors()); + // CPU 개수 * 2 // int cpuCores = Runtime.getRuntime().availableProcessors() * 2; + + // CPU 개수 + 1 int cpuCores = 13; // CPU 개수를 초과하는 데이터를 생성 diff --git a/src/main/java/io/concurrency/chapter01/exam01/ParallelismExample.java b/src/main/java/io/concurrency/chapter01/exam01/ParallelismExample.java index c7a2849..77cddc8 100644 --- a/src/main/java/io/concurrency/chapter01/exam01/ParallelismExample.java +++ b/src/main/java/io/concurrency/chapter01/exam01/ParallelismExample.java @@ -2,10 +2,13 @@ import java.util.ArrayList; import java.util.List; - +/* +* 병렬성 - CPU 개수 >= 작업의 개수에 최적화 되어있습니다. +* */ public class ParallelismExample { public static void main(String[] args) { -// int cpuCores = 1; + System.out.println("CPU 개수 : "+Runtime.getRuntime().availableProcessors()); + int cpuCores = Runtime.getRuntime().availableProcessors(); // CPU 개수만큼 데이터를 생성 @@ -16,7 +19,15 @@ public static void main(String[] args) { // CPU 개수만큼 데이터를 병렬로 처리 long startTime1 = System.currentTimeMillis(); - long sum1 = data.parallelStream() + + /* + * 병렬스트림 진행 (CPU를 전부 사용해서 각각 작업을 하나씩 물기) + * */ +// long sum1 = data.parallelStream() + /* + * 일반스트림 진행 (CPU를 하나만 사용해서 작업을 진행) + * */ + long sum1 = data.stream() .mapToLong(i -> { try { Thread.sleep(500); From 73540bd70feceb38812c443fe44b442b68886d95 Mon Sep 17 00:00:00 2001 From: LimZida Date: Thu, 19 Dec 2024 10:04:27 +0900 Subject: [PATCH 5/7] =?UTF-8?q?20241219=20=EB=B3=91=EB=A0=AC=20=EB=8F=99?= =?UTF-8?q?=EC=8B=9C=EC=84=B1=20=EC=97=B0=EC=8A=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../concurrency/chapter01/exam01/ConcurrencyExample.java | 6 ++++++ .../concurrency/chapter01/exam01/ParallelismExample.java | 9 +++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java b/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java index 7a4d3a1..961090b 100644 --- a/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java +++ b/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java @@ -24,10 +24,16 @@ public static void main(String[] args) { // CPU 개수를 초과하는 데이터를 병렬로 처리 long startTime2 = System.currentTimeMillis(); + /* + * 병렬스트림 진행 (CPU를 전부 사용해서 각각 작업을 하나씩 물기) + * + * 분할정복을 이용하는 forkJoinPool 사용 + * */ long sum2 = data.parallelStream() .mapToLong(i -> { try { Thread.sleep(500); + System.out.println(i + ": " + Thread.currentThread().getName()); } catch (InterruptedException e) { throw new RuntimeException(e); } diff --git a/src/main/java/io/concurrency/chapter01/exam01/ParallelismExample.java b/src/main/java/io/concurrency/chapter01/exam01/ParallelismExample.java index 77cddc8..5f04181 100644 --- a/src/main/java/io/concurrency/chapter01/exam01/ParallelismExample.java +++ b/src/main/java/io/concurrency/chapter01/exam01/ParallelismExample.java @@ -22,15 +22,20 @@ public static void main(String[] args) { /* * 병렬스트림 진행 (CPU를 전부 사용해서 각각 작업을 하나씩 물기) + * + * 분할정복을 이용하는 forkJoinPool 사용 * */ -// long sum1 = data.parallelStream() + long sum1 = data.parallelStream() /* * 일반스트림 진행 (CPU를 하나만 사용해서 작업을 진행) + * + * 순차 작업 진행 * */ - long sum1 = data.stream() +// long sum1 = data.stream() .mapToLong(i -> { try { Thread.sleep(500); + System.out.println(i + ": " + Thread.currentThread().getName()); } catch (InterruptedException e) { throw new RuntimeException(e); } From a86d2f537e0900f31739bcc14415f74d1bc767b5 Mon Sep 17 00:00:00 2001 From: LimZida Date: Thu, 19 Dec 2024 11:01:36 +0900 Subject: [PATCH 6/7] =?UTF-8?q?20241219=20context=20switching=20=EC=97=B0?= =?UTF-8?q?=EC=8A=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chapter01/exam02/ContextSwitchExample.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/concurrency/chapter01/exam02/ContextSwitchExample.java b/src/main/java/io/concurrency/chapter01/exam02/ContextSwitchExample.java index 1c44190..53c4247 100644 --- a/src/main/java/io/concurrency/chapter01/exam02/ContextSwitchExample.java +++ b/src/main/java/io/concurrency/chapter01/exam02/ContextSwitchExample.java @@ -1,5 +1,11 @@ package io.concurrency.chapter01.exam02; - +/* +* 컨텍스트 스위칭 +* +* 프로세스 혹은 스레드 간의 작업 도중에 I/O 혹은 오버헤드가 발생하는 경우, 다른 작업과의 변환을 진행한다. +* +* 해당 과정에서 기존 PCB 혹은 TCB 정보를 삭제하고 캐시를 비운 후, 새로운 PCB 혹은 TCB 정보로 변경된다. +* */ public class ContextSwitchExample { public static void main(String[] args) { From 9d7da5ddd1f6eba1860657403df420ddc7e5655f Mon Sep 17 00:00:00 2001 From: LimZida Date: Thu, 19 Dec 2024 13:34:20 +0900 Subject: [PATCH 7/7] =?UTF-8?q?20241219=20CPU=20,=20IO=20bound=20=ED=95=99?= =?UTF-8?q?=EC=8A=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chapter01/exam03/CPUBoundExample.java | 8 +++++++- .../concurrency/chapter01/exam03/IOBoundExample.java | 12 +++++++++--- .../java/io/concurrency/chapter01/exam03/sample.txt | 1 + 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/concurrency/chapter01/exam03/CPUBoundExample.java b/src/main/java/io/concurrency/chapter01/exam03/CPUBoundExample.java index a20b987..6346cfc 100644 --- a/src/main/java/io/concurrency/chapter01/exam03/CPUBoundExample.java +++ b/src/main/java/io/concurrency/chapter01/exam03/CPUBoundExample.java @@ -6,7 +6,13 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; - +/* +* CPU Bound - 영상처리, AI 작업 관련 +* +* 멀티코어의 병렬성을 이용한다. +* +* CPU 수 >= 스레드 개수 +* */ public class CPUBoundExample { public static void main(String[] args) throws InterruptedException { diff --git a/src/main/java/io/concurrency/chapter01/exam03/IOBoundExample.java b/src/main/java/io/concurrency/chapter01/exam03/IOBoundExample.java index d21be0b..2d8656b 100644 --- a/src/main/java/io/concurrency/chapter01/exam03/IOBoundExample.java +++ b/src/main/java/io/concurrency/chapter01/exam03/IOBoundExample.java @@ -5,10 +5,16 @@ import java.nio.file.Path; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; - +/* +* IO Bound - DB작업이나 파일 읽는 작업 +* +* 멀티스레드의 동시성을 이용한다. +* +* CPU 수 < 스레드 개수 +* */ public class IOBoundExample { public static void main(String[] args) { - int numThreads = Runtime.getRuntime().availableProcessors() / 2; + int numThreads = Runtime.getRuntime().availableProcessors() * 2; ExecutorService executorService = Executors.newFixedThreadPool(numThreads); for (int i = 0; i < numThreads; i++) { @@ -17,7 +23,7 @@ public static void main(String[] args) { // IO 가 집중 되는 작업 for (int j = 0; j < 5; j++) { - Files.readAllLines(Path.of("D:\\lecture\\Java-Concurrency-Programming\\Java-Concurrency-Programming\\src\\chapter01\\exam03\\sample.txt")); + Files.readAllLines(Path.of("C:\\workspace\\java-concurrency-study\\src\\main\\java\\io\\concurrency\\chapter01\\exam03\\sample.txt")); System.out.println("스레드: " + Thread.currentThread().getName() +", " +j); // IO Bound 일때 ContextSwitching 이 일어난다 } diff --git a/src/main/java/io/concurrency/chapter01/exam03/sample.txt b/src/main/java/io/concurrency/chapter01/exam03/sample.txt index e69de29..45ae679 100644 --- a/src/main/java/io/concurrency/chapter01/exam03/sample.txt +++ b/src/main/java/io/concurrency/chapter01/exam03/sample.txt @@ -0,0 +1 @@ +why so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happywhy so happy \ No newline at end of file