이 코드를 통해 교착 상태 예를 찾고 발견되었다여러 객체 트랜잭션에 대한 교착 상태를 피하는 가장 좋은 방법은 무엇입니까?
package com.example.thread.deadlock._synchronized;
public class BankAccount {
double balance;
int id;
BankAccount(int id, double balance) {
this.id = id;
this.balance = balance;
}
void withdraw(double amount) {
// Wait to simulate io like database access ...
try {Thread.sleep(10l);} catch (InterruptedException e) {}
balance -= amount;
}
void deposit(double amount) {
// Wait to simulate io like database access ...
try {Thread.sleep(10l);} catch (InterruptedException e) {}
balance += amount;
}
static void transfer(BankAccount from, BankAccount to, double amount) {
synchronized(from) {
from.withdraw(amount);
synchronized(to) {
to.deposit(amount);
}
}
}
public static void main(String[] args) {
final BankAccount fooAccount = new BankAccount(1, 100d);
final BankAccount barAccount = new BankAccount(2, 100d);
new Thread() {
public void run() {
BankAccount.transfer(fooAccount, barAccount, 10d);
}
}.start();
new Thread() {
public void run() {
BankAccount.transfer(barAccount, fooAccount, 10d);
}
}.start();
}
}
는 교착 상태가 발생하지 않도록 어떻게 transfer
방법을 변경할 것인가? 첫 번째 생각은 모든 계정에 대해 공유 잠금을 만드는 것입니다. 물론 모든 동시성을 없앨 것입니다. 거래에 관련된 두 개의 계정 만 잠그고 다른 계정에는 영향을 미치지 않는 좋은 방법이 있습니까?
다른 거래로 인해 거래하는 동안 1 명의 회원이 돈이 없다면 2 개의 동기화 된 블록을 사용하면 문제가 발생합니다 – Ferrybig
거래가 아니라는 것을 알았습니까? –