강의 노트 (아래 정리 내용 있음 영어 + 한국어)
A. Why we need Threads ?
- 쓰레드가 필요한 이유는 : 응답성과 성능
1. Responsiveness
- Example of poor responsiveness : waiting for customer support
- responsiveness is particulary important for user interface
- Concurrency - multitasking
2. Performance
- can create an illusion of multiple tasks executing in parallel using single core
- with multiple cores, we can truly run tasks completely in parallel
multithreading caveat
- multithreaded programming is fundamentally different from single threaded programming
stack
region in memory, where local variables are stored and passed into functions
instruction pointer
address of the next instruction to execute
Context switch
- Context switch is not cheap and the price of mutitasking (concurrency)
- each thread consumes resources in the cpu and memory
- store data -> restore data when switching thread
- Too many threads = thrashing = spending more time in management than real productive work
- threads consume less resources than processes
- context switching between threads from the same process is cheaper than context switch between different processes
멀티쓰레드 환경 vs 멀티 프로세스로 돌릴지 결정해야하는 중요한 포인트는 ?
멀티 쓰레드 장점 :
- 쓰레드는 서로간의 많은 리소스를 공유함
- 많은 데이터를 서로 공유하면 멀티 쓰레드 애플리케이션 아키텍쳐가 적합함
- 프로세스는 쓰레드 보다 더 큰 단위, 쓰레드는 지웠다 생성하기 더 편리함
- 프로세스 context switching 하는 것 보다 쓰레드 전환이 더 비용이 적음
멀티 프로세스 장점 :
- 효율보단 보안과 안정성이 중요시 할 때
- 각 프로세스는 독립적으로 수행이 되어서 fail over safety 환경이 있음
영어
💡 Why Do We Need Threads?
Threads are essential for improving responsiveness and performance in applications.
🔄 1. Responsiveness
- Enhances the app's ability to remain interactive, even when performing heavy tasks.
- Example: Waiting for customer support (bad responsiveness).
- Particularly important for User Interfaces (UI).
- Achieved through Concurrency (Multitasking).
⚡ 2. Performance
- Threads create the illusion of parallel execution on a single core.
- With multiple cores, tasks can truly run in parallel, boosting performance.
⚠️ Multithreading Caveats
- Multithreaded programming is fundamentally different from single-threaded programming.
- Context Switching:
- When switching between threads, the CPU must store and restore data, which consumes time and memory.
- Too many threads → Thrashing (more time managing threads than executing tasks).
- Thread switching is cheaper than process switching.
🔎 Stack & Instruction Pointer
- Stack: Memory region for storing local variables and function parameters.
- Instruction Pointer: Tracks the next instruction to execute.
🔍 Choosing Between Multithreading and Multiprocessing
✔️ Better for sharing data between tasks. | ✔️ Better for isolation and stability. |
✔️ Lower overhead during context switches. | ✔️ Independent execution improves fault tolerance. |
✔️ Lightweight and easier to create/destroy. | ✔️ Ideal for critical processes needing separation. |
Choose Multithreading for speed and shared memory.
Choose Multiprocessing for security and isolation.
한국어
💡 쓰레드가 필요한 이유
쓰레드는 응답성과 성능 향상을 위해 필수적입니다.
🔄 1. 응답성 (Responsiveness)
- 무거운 작업을 수행하면서도 애플리케이션이 인터랙티브하게 유지될 수 있도록 합니다.
- 예시: 고객 지원을 기다리는 상황 → 응답성이 좋지 않은 경우
- 특히 **UI (User Interface)**에서 중요합니다.
- **동시성 (Concurrency)**을 통해 멀티태스킹이 가능해집니다.
⚡ 2. 성능 (Performance)
- 싱글 코어에서도 동시에 여러 작업이 실행되는 환상을 만들어냅니다.
- 멀티 코어 환경에서는 실제로 여러 작업이 병렬로 실행되어 성능이 향상됩니다.
⚠️ 멀티쓰레딩의 주의점
- 멀티쓰레딩 프로그래밍은 싱글쓰레딩과 근본적으로 다릅니다.
- 컨텍스트 스위칭 (Context Switching):
- 쓰레드 전환 시 데이터를 저장하고 복원하는 작업이 필요합니다.
- 이 과정은 CPU와 메모리 리소스를 소모합니다.
- 쓰레드가 너무 많으면 → 스레싱 (Thrashing) 발생 → 실제 작업보다 관리에 시간이 더 많이 소요됨
- 그러나, 쓰레드 전환은 프로세스 전환보다 비용이 적습니다.
🔎 스택과 명령 포인터 (Stack & Instruction Pointer)
- 스택: 지역 변수와 함수 매개변수가 저장되는 메모리 영역
- 명령 포인터 (Instruction Pointer): 다음에 실행할 명령의 주소를 가리킴
🔍 멀티쓰레드 vs. 멀티프로세스 선택 기준
✔️ 데이터 공유가 많을 때 적합함 | ✔️ 보안과 안정성이 중요한 경우 적합함 |
✔️ 컨텍스트 스위칭 비용이 적음 | ✔️ 각각의 프로세스가 독립적으로 실행되어 안정성 확보 |
✔️ 생성 및 삭제가 가벼움 | ✔️ 장애 발생 시 다른 프로세스에 영향이 없음 |
- 멀티쓰레드 → 속도와 메모리 공유가 중요할 때 선택
- 멀티프로세스 → 보안과 독립성이 중요할 때 선택
'강의 자료 > 고성능을 강조한 Java 멀티스레딩, 병행성 및 병렬 실행 프로그래밍' 카테고리의 다른 글
성능 및 지연 시간 최적화 개요 (2) | 2025.05.25 |
---|---|
섹션 3 : 스레딩 기초 (스레드 연결) (1) | 2025.05.18 |
섹션 3: 스레딩 기초 - 스레드 조정 (0) | 2025.05.18 |
멀티스레드를 활용한 금고 해킹 시뮬레이션 (자바 구현) (2) | 2025.05.18 |
섹션 2: 스레딩 기초 - 스레드 생성 (스레드의 기능 + 스레드 상속) (2) | 2025.05.18 |