"κ΅μ°©μν(Deadlock)κ° λ¬΄μμ΄λ©°, λ°μ 쑰건과 ν΄κ²° λ°©λ²μ λν΄ μ€λͺ ν΄μ£ΌμΈμ."
κ΅μ°©μν(Deadlock)λ λ κ° μ΄μμ νλ‘μΈμ€λ μ€λ λκ° μλ‘κ° κ°μ§ μμμ κΈ°λ€λ¦¬λ©° 무νν λκΈ°νλ μνλ₯Ό μλ―Έν©λλ€.
// κ΅μ°©μν λ°μ κ°λ₯ν μ½λ
public class DeadlockExample {
private static Object lock1 = new Object();
private static Object lock2 = new Object();
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
synchronized (lock1) {
System.out.println("Thread 1: Holding lock1");
try { Thread.sleep(100); } catch (InterruptedException e) {}
synchronized (lock2) {
System.out.println("Thread 1: Holding lock2");
}
}
});
Thread thread2 = new Thread(() -> {
synchronized (lock2) {
System.out.println("Thread 2: Holding lock2");
try { Thread.sleep(100); } catch (InterruptedException e) {}
synchronized (lock1) {
System.out.println("Thread 2: Holding lock1");
}
}
});
thread1.start();
thread2.start();
}
}
-
μνΈ λ°°μ (Mutual Exclusion)
- μμμ ν λ²μ ν νλ‘μΈμ€λ§ μ¬μ© κ°λ₯
synchronized void transfer(Account from, Account to, double amount) { // κ³μ’ μμ‘ μ΄μ²΄ λ‘μ§ }
-
μ μ μ λκΈ°(Hold and Wait)
- μμμ 보μ ν μνμμ λ€λ₯Έ μμμ μμ²
synchronized (account1) { synchronized (account2) { // λ κ³μ’λ₯Ό λͺ¨λ νμλ‘ νλ μμ } }
-
λΉμ μ (No Preemption)
- λ€λ₯Έ νλ‘μΈμ€μ μμμ κ°μ λ‘ λΉΌμμ μ μμ
// μμμ κ°μ λ‘ ν΄μ ν μ μλ μν© public void processData() { lock.lock(); // νλ² νλν λ½μ μ§μ ν΄μ νκΈ° μ κΉμ§ μ μ§ try { // μ²λ¦¬ λ‘μ§ } finally { lock.unlock(); } }
-
μν λκΈ°(Circular Wait)
- νλ‘μΈμ€κ° μμ μμ²μ΄ μν ννλ‘ λ°μ
// μν λκΈ° λ°μ κ°λ₯ μ½λ Thread 1: lock(A) β wait(B) Thread 2: lock(B) β wait(A)
// μμνλ λ½ νλμΌλ‘ κ΅μ°©μν μλ°©
public class Account {
private static final Object tieLock = new Object();
public void transfer(Account from, Account to, double amount) {
// κ³μ’ IDλ₯Ό κΈ°μ€μΌλ‘ λ½ νλ μμ μ ν¨
if (from.getId() < to.getId()) {
synchronized (from) {
synchronized (to) {
// μ΄μ²΄ λ‘μ§
}
}
} else if (from.getId() > to.getId()) {
synchronized (to) {
synchronized (from) {
// μ΄μ²΄ λ‘μ§
}
}
} else {
synchronized (tieLock) {
synchronized (from) {
synchronized (to) {
// μ΄μ²΄ λ‘μ§
}
}
}
}
}
}
// μνμ μκ³ λ¦¬μ¦ κ΅¬ν μμ
public class BankerAlgorithm {
private int[] available;
private int[][] maximum;
private int[][] allocation;
private int[][] need;
public boolean isSafeState(int[] request, int processId) {
// μμ μν κ²μ¬ λ‘μ§
return true; // μμ ν κ²½μ°μλ§ μμ ν λΉ
}
}
// λ°λλ½ νμ§ μμ
public class DeadlockDetector {
private Set<Thread> findDeadlockedThreads() {
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long[] threadIds = threadBean.findDeadlockedThreads();
Set<Thread> deadlockedThreads = new HashSet<>();
if (threadIds != null) {
ThreadInfo[] threadInfos = threadBean.getThreadInfo(threadIds);
for (ThreadInfo threadInfo : threadInfos) {
Thread thread = findMatchingThread(threadInfo.getThreadId());
if (thread != null) {
deadlockedThreads.add(thread);
}
}
}
return deadlockedThreads;
}
}
public class SafeBankTransfer {
private final Lock lock = new ReentrantLock();
public void transfer(Account from, Account to, double amount) {
Lock firstLock = from.getId() < to.getId() ? from.getLock() : to.getLock();
Lock secondLock = from.getId() < to.getId() ? to.getLock() : from.getLock();
firstLock.lock();
try {
secondLock.lock();
try {
// μμ ν μ΄μ²΄ λ‘μ§
} finally {
secondLock.unlock();
}
} finally {
firstLock.unlock();
}
}
}
public class TimeoutLocking {
private final Lock lock = new ReentrantLock();
public boolean executeWithTimeout() {
try {
if (lock.tryLock(1000, TimeUnit.MILLISECONDS)) {
try {
// μμ
μν
return true;
} finally {
lock.unlock();
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}
}
- "System Deadlocks" by Coffman et al.
- ACM Computing Surveys
- "Detection of Mutual Exclusion Using Static Analysis"
- IEEE Proceedings
- Operating System Concepts (Silberschatz)
- Chapter 7: Deadlocks
- Modern Operating Systems (Tanenbaum)
-
"μ€μ νλ‘μ νΈμμ λ°λλ½μ κ²½νν μ μ΄ μλμ? μ΄λ»κ² ν΄κ²°νμ ¨λμ?"
-
"λ°λλ½κ³Ό λΌμ΄λΈλ½(Livelock)μ μ°¨μ΄μ μ 무μμΈκ°μ?"
-
"λΆμ° μμ€ν μμμ λ°λλ½μ μ΄λ»κ² ν΄κ²°ν μ μμκΉμ?"
-
"λ°μ΄ν°λ² μ΄μ€ νΈλμμ μμ λ°μνλ λ°λλ½μ μ΄λ»κ² ν΄κ²°νμλμ?"
μ€μ λ©΄μ μμλ μ΄λ‘ μ μΈ μ§μλΏλ§ μλλΌ, μ€μ κ²½νν λ°λλ½ μν©κ³Ό ν΄κ²° λ°©λ²μ λν΄ κ΅¬μ²΄μ μΌλ‘ μ€λͺ νλ κ²μ΄ μ’μ΅λλ€.