-
[Java] μλ° μ°λ λ κ΅μ°©μν(deadlock)Android/Java 2021. 2. 7. 17:01λ°μν
- μ΄ κΈμ "μλ° μ¨λΌμΈ μ€ν°λ" λ΄μ©μ κ°μ§κ³ 곡λΆνμ¬ μμ±ν κΈμ λλ€.
Thread DeadLock μ΄λ?
λ©ν° μ°λ λ νλ‘κ·Έλλ°μμ λκΈ°νλ₯Ό ν΅ν΄ λ½μ νλνμ¬ λμΌν μμμ μ¬λ¬ κ³³μμ ν¨λΆλ‘ μ¬μ©νμ§ λͺ»νλλ‘ νμμ΅λλ€. νμ§λ§ λ κ°μ μ°λ λμμ μλ‘κ° κ°μ§κ³ μλ λ½μ΄ ν΄μ λκΈ°λ₯Ό κΈ°λ€λ¦¬λ μνκ° μκΈΈ μ μμΌλ©° μ΄λ¬ν μνλ₯Ό κ΅μ°©μν(deadlock) μ΄λΌκ³ ν©λλ€. κ΅μ°©μνκ° λλ©΄ μ΄λ€ μμ λ μ€νλμ§ λͺ»νκ³ μλ‘ μλλ°©μ μμ μ΄ λλκΈ°λ§ λ°λΌλ 무νμ λκΈ° μνμ λλ€.
DeakLock λ°μ 쑰건
μνΈ λ°°μ (Mutual Exclusion) : ν μμμ λν΄ μ¬λ¬ μ°λ λ λμ μ κ·Ό λΆκ°
μ μ μ λκΈ° (Hold and Wait) : μμμ κ°μ§κ³ μλ μνμμ λ€λ₯Έ μ°λ λκ° μ¬μ©νκ³ μλ μμ λ°λ©μ κΈ°λ€λ¦¬λ κ²
λΉμ μ (Non Preemptive) : λ€λ₯Έ μ°λ λμ μμμ μ€ν μ€κ°μ κ°μ λ‘ κ°μ Έμ¬ μ μμ
ννλκΈ° (Circle Wait) : κ° μ°λ λκ° μνμ μΌλ‘ λ€μ μ°λ λκ° μꡬνλ μμμ κ°μ§κ³ μλ κ²
μμ 4κ°μ§ 쑰건μ λͺ¨λ μΆ©μ‘±ν κ²½μ° λ°λλ½μ΄ λ°μνκ² λ©λλ€. λ°λλ‘ λ§νλ©΄, μ 4κ°μ§ μ€ νλλΌλ μΆ©μ‘±νμ§ μμ κ²½μ° λ°λλ½μ ν΄κ²°ν μ μλ€λ λ»μ΄κΈ°λ ν©λλ€.
DeakLock μ€μ΅ μ½λ
public class Main { public static Object object1 = new Object(); public static Object object2 = new Object(); public static void main(String[] args) { FirstThread thread1 = new FirstThread(); SecondThread thread2 = new SecondThread(); thread1.start(); thread2.start(); } private static class FirstThread extends Thread{ @Override public void run() { synchronized (object1){ System.out.println("First Thread has object1's lock"); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("First Thread want to have object2's lock. so wait"); synchronized (object2){ System.out.println("First Thread has object2's lock too"); } } } } private static class SecondThread extends Thread{ @Override public void run() { synchronized (object2){ System.out.println("Second Thread has object2's lock"); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Second Thread want to have object1's lock, so wait"); synchronized (object1){ System.out.println("Second Thread has object1's lock too"); } } } } }
μνΈ λ°°μ : object1κ³Ό object2 κ°μ²΄μ λν΄μ λμμ μ°λ λκ° μ¬μ©ν μ μλλ‘ νμμ΅λλ€.
μ μ μ λκΈ° : FirstThreadμμλ object1μ λ½μ κ°μ§κ³ μμΌλ©΄μ object2μ λν λ½μ μνκ³ , SecondThreadλ object2μ λν λ½μ κ°μ§κ³ μμΌλ©΄μ object1μ λ½μ νλνκΈ°λ₯Ό μν©λλ€.
λΉμ μ : μ°λ λμ μ°μ μμμ κΈ°λ³Έκ°μ NORM_PRIORITYλ‘ λμΌνκ² μ€μ λμ΄ μμ΅λλ€.
ννλκΈ° : FirstThreadλ SecondThreadμ object2 κ°μ²΄μ λ½μ λκΈ°νκ³ SecondThreadλ FirstThreadμ object1 κ°μ²΄μ λ½μ λκΈ°νκ³ μμ΅λλ€.
μ€νκ²°κ³Όμ²λΌ λ°λλ½μ΄ λ°μνμ¬ μλ¬΄λ° μ€ννμ§ λͺ»ν μ± λ¬΄νμ λκΈ°νκ³ μμ΅λλ€.
public class Main { public static Object object1 = new Object(); public static Object object2 = new Object(); public static void main(String[] args) { FirstThread thread1 = new FirstThread(); SecondThread thread2 = new SecondThread(); thread1.start(); thread2.start(); } private static class FirstThread extends Thread{ @Override public void run() { synchronized (object1){ System.out.println("First Thread has object1's lock"); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("First Thread want to have object2's lock. so wait"); synchronized (object2){ System.out.println("First Thread has object2's lock too"); } } } } private static class SecondThread extends Thread{ @Override public void run() { synchronized (object1){ System.out.println("Second Thread has object2's lock"); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Second Thread want to have object1's lock, so wait"); synchronized (object2){ System.out.println("Second Thread has object1's lock too"); } } } } }
λ°λλ½μ ν΄κ²°νκΈ° μν΄μλ 4κ°μ§ 쑰건 μ€ νλλ§ μΆ©μ‘±λμ§ μκ² νλ€λ©΄ ν΄κ²°ν μ μμ΅λλ€.
μ μ½λλ ννλκΈ° 쑰건μ λ§μ‘±νκΈ° μκΈ° λλ¬Έλ° λ°λλ½μ΄ λ°μνμ§ μλλ€λ κ²μ νμΈν μ μμ΅λλ€.λ°μν'Android > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Java] Annotation, Meta dataλ? (0) 2021.02.19 [Java] Enumμ΄λ? (0) 2021.02.14 [Java] μλ° μ°λ λ μν, μ°μ μμ(State, Priority) (0) 2021.02.05 [Java] μλ° μ°λ λ μμ±(Thread, Runnable) (4) 2021.02.02 [Java] μλ° μμΈμ²λ¦¬ (0) 2021.02.01