ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [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가지 쑰건 쀑 ν•˜λ‚˜λ§Œ μΆ©μ‘±λ˜μ§€ μ•Šκ²Œ ν•œλ‹€λ©΄ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    μœ„ μ½”λ“œλŠ” ν™˜ν˜•λŒ€κΈ° 쑰건을 λ§Œμ‘±ν•˜κΈ° μ•ŠκΈ° λ•Œλ¬Έλ° λ°λ“œλ½μ΄ λ°œμƒν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” 것을 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

    λ°˜μ‘ν˜•
Designed by Tistory.