Thread

[size=x-large]
public class Bank implements Runnable {
private int money;
Thread th=new Thread();
public void run() {

for(int i = 0 ;i<12;i++){
int temp ;
temp=money;
money+=1000;
System.out.println( th.currentThread().getName()+"余的钱"+money );
}
try {
th.sleep(10);
} catch (InterruptedException e) {

e.printStackTrace();
}
}
public static void main(String []args){
Bank b=new Bank();
Thread th1=new Thread (b,"爸爸存入了1000");
Thread th2= new Thread(b,"妈妈存入了1000");
th1.start();
th2.start();

}
}


public class Test2 extends Thread {
private static int money;

public synchronized void run() {
for (int i = 0; i < 12; i++) {
int temp;
money += 1000;
temp = money;
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("存款" + money + "余的钱" + temp);
}
}

public static void main(String[] args) {
Test2 t = new Test2();
t.start();
Test2 t2 = new Test2();
t2.start();
}

}


用synchronized可能会产生死锁。


[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值