diff --git a/README.md b/README.md index ba4acf1..9714420 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ # Java-Concurrency-Programming +장수원님의 자바 동시성 프로그래밍 [리액티브 프로그래밍 Part.1] 에 대한 스터디 목적의 Repo입니다. + +모든 코드의 저작권은 https://github.com/onjsdnjs/Java-Concurrency-Programming 에 존재합니다. diff --git a/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java b/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java index dcae22a..961090b 100644 --- a/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java +++ b/src/main/java/io/concurrency/chapter01/exam01/ConcurrencyExample.java @@ -2,12 +2,19 @@ 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; - int cpuCores = Runtime.getRuntime().availableProcessors() * 2; -// int cpuCores = 13; + // CPU 개수 + 1 + int cpuCores = 13; // CPU 개수를 초과하는 데이터를 생성 List data = new ArrayList<>(); @@ -17,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 c7a2849..5f04181 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,10 +19,23 @@ public static void main(String[] args) { // CPU 개수만큼 데이터를 병렬로 처리 long startTime1 = System.currentTimeMillis(); + + /* + * 병렬스트림 진행 (CPU를 전부 사용해서 각각 작업을 하나씩 물기) + * + * 분할정복을 이용하는 forkJoinPool 사용 + * */ long sum1 = data.parallelStream() + /* + * 일반스트림 진행 (CPU를 하나만 사용해서 작업을 진행) + * + * 순차 작업 진행 + * */ +// long sum1 = data.stream() .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/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) { 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