자바 멀티 스레딩을 배우고 있습니다. 이것은 내가 작성한 코드입니다.자바가 변수를 보호하지 않고 동기화되었습니다
package com.company;
public class Account {
private double balance = 100;
public synchronized double getBalance() {
synchronized (this){
return balance;
}
}
public void setBalance(double balance) {
this.balance = balance;
}
public boolean withdraw(double amount,String name){
if(this.getBalance()>amount){
if(Thread.currentThread().getName().equals("customer0")){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.setBalance(this.getBalance() - amount);
System.out.println(Thread.currentThread().getName() + " withdraw " + amount);
System.out.println("Hello, " + Thread.currentThread().getName() + " You current balance is " + this.getBalance());
return true;
}
else{
System.out.println("Sorry, " + Thread.currentThread().getName() + ". Your current balance is " + this.getBalance() + " and you cannot withdraw " + amount);
//System.out.println("Welcome, " + Thread.currentThread().getName() + " You current balance is " + this.getBalance());
return false;
}
}
}
메인 클래스는
package com.company;
import org.omg.PortableServer.THREAD_POLICY_ID;
public class Main implements Runnable {
Account account = new Account();
public static void main(String[] args){
Main main = new Main();
for(int i= 0; i< 2; i++) {
Thread c1 = new Thread(main, "customer" + i);
c1.start();
}
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "'s balance is " + account.getBalance());
account.withdraw(60, Thread.currentThread().getName());
//
}
}
나는 그것이 하나 개의 스레드 만 한 번으로 액세스 할 수 있는지 확인하기 위해 getBalance()에서 키워드 동기화 넣어. 그러나 여전히 나는 부정적 균형을 얻습니다.
customer0's balance is 100.0
customer1's balance is 100.0
customer1 withdraw 60.0
Hello, customer1 You current balance is 40.0
customer0 withdraw 60.0
Hello, customer0 You current balance is -20.0
내가 뭘 잘못 했니?
기록을 동기화해야하지 않습니까? –
참고로, money와 같은 중요한 중요 값에는'double'을 사용하지 마십시오. –
Juan이 언급했듯이 get을 동기화하는 대신 쓰기 작업을 동기화해야합니다. 동기화 할 때 사용할 필요가 없습니다. getBalance – Wael