package syn;
public class bbb implements Runnable{
aaa a=new aaa();
@Override
public void run() {
//使用synchronized完成线程同步安全
synchronized(a){
//判断余额是否>400
if(a.balance>=400){
a.show();
}else{
System.out.println("余额不足");
}
}
}
}
class aaa{
//定义有600元
int balance=600;
//每次取400元的方法
public int show(){
balance-=400;
System.out.println(Thread.currentThread().getName()+"取走400,余额"+balance);
return balance;
}
}
package syn;
public class Test {
public static void main(String[] args) {
Runnable r=new bbb();
Thread t=new Thread(r,"张三");
Thread t1=new Thread(r,"张三他媳妇");
t.start();
t1.start();
}
}