线程安全
同步代码块
synchronized ("锁") { //同步方法
if(count > 0){
System.out.println(Thread.currentThread().getName()+"取走1000元,还剩"+(count - 1000)+"元");
count = count - 1000;
}else{
System.out.println("余额不足,无法提现");
break;
}
}
同步函数
public static synchronized void getMoney(){ //同步函数
while(true){
//synchronized ("锁") { //同步方法
if(count > 0){
System.out.println(Thread.currentThread().getName()+"取走1000元,还剩"+(count - 1000)+"元");
count = count - 1000;
}else{
System.out.println("余额不足,无法提现");
break;
}
//}
}
}