-
Notifications
You must be signed in to change notification settings - Fork 93
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
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
11μ₯/84_νλ‘κ·Έλ¨μ_λμμ_μ€λ λ_μ€μΌμ€λ¬μ_κΈ°λμ§_λ§λΌ_μ΄μ£Όν.md
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,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μ μ€λ λ μ°μ μμμ μμ‘΄ν΄μλ μ λλ€. | ||
- μ΄ κΈ°λ₯λ€μ μ€λ λ μ€μΌμ€λ¬μ μ 곡νλ ννΈμΌ λΏμ΄λ€. | ||
- μ€λ λ μ°μ μμλ μ΄λ―Έ μ λμνλ νλ‘κ·Έλ¨μ μλΉμ€ νμ§μ λμ΄κΈ° μν΄ λλ¬Όκ² μ°μΌ μλ μμ§λ§, κ°μ ν λμνλ νλ‘κ·Έλ¨μ 'κ³ μΉλ μ©λ'λ‘ μ¬μ©ν΄μλ βμ λβ μ λλ€. |