generated from Learning-Is-Vital-In-Development/study-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# 자바 동시성 | ||
|
||
# 자바에서 사용할 수 있는 동시성 문제를 해결하는 방법 몇가지 | ||
|
||
## 1. synchronized | ||
|
||
특정 메서드나 블럭에 lock을 걸어 동시성 문제를 해결합니다. 하지만 범위가 큰 경우가 많기 때문에 성능문제(데드락)가 발생할 여지가 높습니다. | ||
|
||
보통 메서드, 객체에 `synchronized` 키워드를 사용해 잠금을 겁니다. 접근에 성공한 스레드는 잠금을 획득하고 해당 메서드나 블락이 종료될 때 까지 다른 스레드에서 접근이 불가능하게 됩니다. | ||
|
||
```java | ||
int count = 0; | ||
public synchronized void increment() { | ||
count++; | ||
} | ||
``` | ||
|
||
## 2. Volatile | ||
|
||
Java에서 가시성(visibility)을 보장하며 변수단위로 키워드(`volatile`)를 선언해 값을 CPU 캐시가 아닌 메인 메모리에서 직접 읽고 씁니다. 이를 통해 변경 내용을 즉시 반영하는 원리입니다. | ||
|
||
```java | ||
private volatile int count = 0; | ||
``` | ||
|
||
## 3. Atomic | ||
|
||
Java에서 원자성을 보장하는 클래스로 값을 읽고 쓰는 연산을 원자적으로 수행, 다른 스레드가 동시에 접근해도 이전 스레드의 연산이 완전히 종료된 이후 접근을 허용합니다. | ||
|
||
CAS(Compare-And-Swap)알고리즘을 사용하며, 이 알고리즘은 변수의 값을 읽고 쓰는 동안 다른 스레드가 해당 변수의 값을 변경하지 않았는지 확인하고 변경이 없는 경우에만 값을 설정합니다. | ||
|
||
```java | ||
private AtomicInteger count = new AtomicInteger(0); | ||
``` | ||
|
||
## 4. Semaphore | ||
|
||
개수를 나타내는 값을 가지며, 스레드가 접근하는 경우 값을 -1 씩 줄여 0 이되면 접근을 차단하며, 다른 스레드들은 세마포어가 릴리즈될 때까지 대기합니다. | ||
|
||
Java 에서는 `Semaphore` 클래스로 지원합니다. | ||
`acquire()` : 접근을 시도하며 접근에 성공한 경우 세마포어의 값을 감소하고 리소스를 획득합니다. | ||
|
||
`release()` : 스레드의 연산이 종료된 후 허용된 개수를 증가시키며 다른 스레드가 리소스에 접근할 수 있도록 허용합니다. | ||
|
||
```java | ||
private Semaphore semaphore = new Semaphore(1); | ||
``` | ||
|
||
## 5. Mutex | ||
|
||
공유 자원에 대한 동시 접근을 막고, 한번에 하나의 스레드만 허용하는 기능(상호배제)을 제공하는 기법으로 Java 에서는 `ReentrantLock` 클래스로 구현할 수 있습니다. | ||
|
||
`lock()` : 뮤텍스를 획득하고 잠금처리 | ||
|
||
`unlock()` : 뮤텍스를 반환하고 잠금해제 | ||
|
||
```java | ||
private Lock mutex = new ReentrantLock(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# IntelliJ 프로파일링 | ||
|
||
# 개요 | ||
|
||
IntelliJ 프로파일러를 통해서 jvm 애플리케이션의 프로파일링 확인해보기 | ||
|
||
# 왜 IntelliJ Profiler 인가? | ||
|
||
다른 에이전트나 프로그램을 설치할 필요없이, 개발환경에서 바로 실행하고 확인해 볼 수 있는 접근성이 뛰어나며, 결과를 프로파일링 종료 후 바로 확인이 가능함 | ||
|
||
물론 운영 환경에선 다른 상용 프로파일러를 사용해야 하지만 프로파일링을 간편히 확인해볼 수 있다는 점에서 토이프로젝트나 개발환경에서 간단한 디버깅용으로 쓰기 좋을거 같다. | ||
|
||
## 시작하기 | ||
|
||
먼저 IntelliJ → View → Tool Windows → Profiler 로 콘솔창에서 조회할 수 있다. | ||
|
||
실행은 프로그램 실행하는 run → Profile ‘애플리케이션명’ with IntelliJ Profiler 를 선택해서 실행하게 되면 프로파일링을 시작하며 Stop으로 중지하면 프로파일링을 종료하고 바로 show result로 볼 수 있다. | ||
|
||
 | ||
|
||
## 현재 프로세스 조회하기 | ||
|
||
먼저 콘솔창 위치에 있는 Profiler 창이 있다면 Home → Process → CPU and Memory Live Charts 로 현재 실행중인 프로세스의 cpu, memory, threads 상태들을 확인해 볼 수 있다. | ||
|
||
 | ||
|
||
## Heap Dump | ||
|
||
콘솔창 옆에 카메라 모양 아이콘을 보면 | ||
|
||
- Capture Memory Snapshot, | ||
|
||
 | ||
|
||
- Get Thread Dump | ||
|
||
 | ||
|
||
|
||
두가지 정보를 얻을 수 있다. | ||
|
||
## 프로파일링 결과보기 | ||
|
||
프로파일링 결과를 확인하게 되면 애플리케이션명과 프로파일링한 일시가 적힌 탭을 눌러서 확인해 볼 수 있다. | ||
|
||
- Flame Graph : 전체적으로 프로파일링된 클래스, 메서드와 메모리 상태, 호출된 스택을 확인해 볼 수 있다. | ||
|
||
 | ||
|
||
|
||
- Call Tree : 샘플링된 데이터를 트리 구조로 구성하며 실행 시간, 재귀 호출, 필퍼링된 호출들을 확인해 볼 수 있다. | ||
|
||
 | ||
|
||
- Method List : 프로필 데이터의 모든 분석법을 수집하고 누적 샘플 시간을 기준으로 정렬하며, 호출자의 계층 구조, 호출 수신자, 호출 수신자 리스트를 볼 수 있다. | ||
|
||
 | ||
|
||
- Time line : 개별 스레드의 활동을 모니터링하는 기능으로 시간 경과에 따른 CPU 활동과 메모리 할당을 시각화해서 보여준다. 가장 활동적인 스레드, 동시성 문제 확인, IO 프로파일등을 확인할 수 있다. | ||
|
||
 | ||
|
||
- Events : 애플리케이션, JVM, OS등 프로세스와 연관된 이벤트 처리목록을 조회할 수 있다. | ||
|
||
|
||
참조 | ||
|
||
- https://www.jetbrains.com/help/idea/read-the-profiling-report.html#profiler-flame-chart | ||
- https://gomnezip.tistory.com/396 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.