Skip to content

Commit

Permalink
add Item 84
Browse files Browse the repository at this point in the history
  • Loading branch information
JuHyun419 committed Apr 6, 2021
1 parent c1e3e9d commit 8ee6520
Showing 1 changed file with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
## Item 84 ν”„λ‘œκ·Έλž¨μ˜ λ™μž‘μ„ μŠ€λ ˆλ“œ μŠ€μΌ€μ€„λŸ¬μ— κΈ°λŒ€μ§€ 말라
- μ—¬λŸ¬ μŠ€λ ˆλ“œκ°€ μ‹€ν–‰ 쀑이면 운영체제의 μŠ€λ ˆλ“œ μŠ€μΌ€μ€„λŸ¬κ°€ μ–΄λ–€ μŠ€λ ˆλ“œλ₯Ό μ–Όλ§ˆλ‚˜ 였래 싀행할지 μ •ν•œλ‹€.
- 정상적인 운영체제라면 이 μž‘μ—…μ„ κ³΅μ •ν•˜κ²Œ μˆ˜ν–‰ν•˜μ§€λ§Œ ꡬ체적인 μŠ€μΌ€μ€„λ§ 정책은 μš΄μ˜μ²΄μ œλ§ˆλ‹€ λ‹€λ₯Ό 수 μžˆλ‹€.
- **μ •ν™•μ„±μ΄λ‚˜ μ„±λŠ₯이 μŠ€λ ˆλ“œ μŠ€μΌ€μ€„λŸ¬μ— 따라 λ‹¬λΌμ§€λŠ” ν”„λ‘œκ·Έλž¨μ΄λΌλ©΄ λ‹€λ₯Έ ν”Œλž«νΌμ— μ΄μ‹ν•˜κΈ° μ–΄λ ΅λ‹€.**
- κ²¬κ³ ν•˜κ³  λΉ λ¦Ών•˜κ³  이식성 쒋은 ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λŠ” κ°€μž₯ 쒋은 방법은 **μ‹€ν–‰ κ°€λŠ₯ν•œ μŠ€λ ˆλ“œμ˜ 평균적인 수λ₯Ό ν”„λ‘œμ„Έμ„œ μˆ˜λ³΄λ‹€ μ§€λ‚˜μΉ˜κ²Œ λ§Žμ•„μ§€μ§€ μ•Šλ„λ‘ ν•˜λŠ” 것이닀.**
- μ‹€ν–‰ κ°€λŠ₯ν•œ μŠ€λ ˆλ“œ 수λ₯Ό 적게 μœ μ§€ν•˜λŠ” μ£Όμš” 기법은 각 μŠ€λ ˆλ“œκ°€ 무언가 μœ μš©ν•œ μž‘μ—…μ„ μ™„λ£Œν•œ ν›„μ—λŠ” λ‹€μŒ 일거리가 생길 λ•ŒκΉŒμ§€ λŒ€κΈ°ν•˜λ„λ‘ ν•˜λŠ” 것이닀.
- **μŠ€λ ˆλ“œλŠ” λ‹Ήμž₯ μ²˜λ¦¬ν•΄μ•Ό ν•  μž‘μ—…μ΄ μ—†λ‹€λ©΄ μ‹€ν–‰λΌμ„œλŠ” μ•ˆ λœλ‹€.**
- μŠ€λ ˆλ“œλŠ” μ ˆλŒ€ λ°”μœ λŒ€κΈ°(busy waiting) μƒνƒœκ°€ 되면 μ•ˆ λœλ‹€.
- 곡유 객체의 μƒνƒœκ°€ λ°”λ€” λ•ŒκΉŒμ§€ μ‰¬μ§€μ•Šκ³  κ²€μ‚¬ν•΄μ„œλŠ” μ•ˆ λœλ‹€λŠ” λœ»μ΄λ‹€.


<br>

### λ°”μœ λŒ€κΈ°(Busy Waiting)

```html
λ°”μœ λŒ€κΈ°(μ˜μ–΄: busy waiting λ˜λŠ” spinning)λž€ μ–΄λ– ν•œ νŠΉμ • κ³΅μœ μžμ›μ— λŒ€ν•˜μ—¬ 두 개 μ΄μƒμ˜ ν”„λ‘œμ„ΈμŠ€λ‚˜ μŠ€λ ˆλ“œκ°€
κ·Έ 이용 κΆŒν•œμ„ νšλ“ν•˜κ³ μž ν•˜λŠ” 동기화 μƒν™©μ—μ„œ κ·Έ κΆŒν•œ νšλ“μ„ μœ„ν•œ κ³Όμ •μ—μ„œ μΌμ–΄λ‚˜λŠ” ν˜„μƒμ΄λ‹€.
λŒ€λΆ€λΆ„μ˜ κ²½μš°μ— μŠ€ν•€λ½(Spin-lock)κ³Ό 이것을 λ™μΌν•˜κ²Œ μƒκ°ν•˜μ§€λ§Œ, μ—„λ°€νžˆ λ§ν•˜μžλ©΄ μŠ€ν•€λ½μ΄ λ°”μœ λŒ€κΈ° κ°œλ…μ„ μ΄μš©ν•œ 것이닀.

https://ko.wikipedia.org/wiki/%EB%B0%94%EC%81%9C_%EB%8C%80%EA%B8%B0


λ°”μœ λŒ€κΈ°(Busy Waiting)
μ›ν•˜λŠ” μžμ›μ„ μ–»κΈ° μœ„ν•΄ κΈ°λ‹€λ¦¬λŠ” 것이 μ•„λ‹ˆλΌ κΆŒν•œμ„ 얻을 λ•ŒκΉŒμ§€ ν™•μΈν•˜λŠ” 것
=> κΆŒν•œ νšλ“μ„ μœ„ν•΄ λ§Žμ€ CPUλ₯Ό λ‚­λΉ„ν•œλ‹€λŠ” 단점이 μ‘΄μž¬ν•¨
```

https://simsimjae.tistory.com/289

<br>

### Thread.yield
- λ‹€λ₯Έ μ“°λ ˆλ“œμ—κ²Œ μž‘μ—…(μ‹€ν–‰)을 μ–‘λ³΄ν•˜κ³  μ‹€ν–‰ λŒ€κΈ° μƒνƒœκ°€ λœλ‹€.

```java
class MyThread extends Thread {

@Override
public void run() {
for (int i = 0; i < 5; i++)
System.out.println(Thread.currentThread().getName() + " in control");
}
}

public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();

for (int i = 0; i < 5; i++) {
// Control passes to child thread
Thread.yield();

// After execution of child Thread
// main thread takes over
System.out.println(Thread.currentThread().getName() + " in control");
}
}
}


main in control
Thread-0 in control
Thread-0 in control
Thread-0 in control
Thread-0 in control
Thread-0 in control
main in control
main in control
main in control
main in control


main in control
main in control
Thread-0 in control
Thread-0 in control
Thread-0 in control
main in control
Thread-0 in control
Thread-0 in control
main in control
main in control

```

![image](https://user-images.githubusercontent.com/50076031/113724561-6fcd9a80-972d-11eb-825e-6c890c573e78.png)

https://www.geeksforgeeks.org/java-concurrency-yield-sleep-and-join-methods/

- νŠΉμ • μŠ€λ ˆλ“œκ°€ λ‹€λ₯Έ μŠ€λ ˆλ“œλ“€κ³Ό 비ꡐ해 CPU μ‹œκ°„μ„ μΆ©λΆ„νžˆ 얻지 λͺ»ν•΄μ„œ κ°„μ‹ νžˆ λŒμ•„κ°€λŠ” ν”„λ‘œκ·Έλž¨μ„ 보더라도 **Thread.yieldλ₯Ό μ¨μ„œ 문제λ₯Ό κ³ μ³λ³΄λ €λŠ” μœ ν˜Ήμ„ λ–¨μ³λ‚΄μž.**
- 증상이 μ–΄λŠμ •λ„ ν˜Έμ „λ  수 μžˆμœΌλ‚˜ 이식성은 그렇지 μ•Šμ„ 것이닀.
- Thread.yieldλŠ” ν…ŒμŠ€νŠΈν•  μˆ˜λ‹¨λ„ μ—†λ‹€.
- 차라리 μ• ν”Œλ¦¬μΌ€μ΄μ…˜ ꡬ쑰λ₯Ό λ°”κΏ” λ™μ‹œμ— μ‹€ν–‰ κ°€λŠ₯ν•œ μŠ€λ ˆλ“œ μˆ˜κ°€ 적어지도둝 μ‘°μΉ˜ν•΄μ£Όμž.

<br>

### 핡심 정리
- ν”„λ‘œκ·Έλž¨μ˜ λ™μž‘μ„ μŠ€λ ˆλ“œ μŠ€μΌ€μ€„λŸ¬μ— κΈ°λŒ€μ§€ 말자.
- 견고성과 이식성을 λͺ¨λ‘ ν•΄μΉ˜λŠ” ν–‰μœ„λ‹€.
- 같은 이유둜, Thread.yield와 μŠ€λ ˆλ“œ μš°μ„ μˆœμœ„μ— μ˜μ‘΄ν•΄μ„œλ„ μ•ˆ λœλ‹€.
- 이 κΈ°λŠ₯듀은 μŠ€λ ˆλ“œ μŠ€μΌ€μ€„λŸ¬μ— μ œκ³΅ν•˜λŠ” 힌트일 뿐이닀.
- μŠ€λ ˆλ“œ μš°μ„ μˆœμœ„λŠ” 이미 잘 λ™μž‘ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ˜ μ„œλΉ„μŠ€ ν’ˆμ§ˆμ„ 높이기 μœ„ν•΄ λ“œλ¬Όκ²Œ 쓰일 μˆ˜λŠ” μžˆμ§€λ§Œ, κ°„μ‹ νžˆ λ™μž‘ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ 'κ³ μΉ˜λŠ” μš©λ„'둜 μ‚¬μš©ν•΄μ„œλŠ” βœμ ˆλŒ€βœ μ•ˆ λœλ‹€.

0 comments on commit 8ee6520

Please sign in to comment.