public class Test {
public static void main(String[] args) {
Account account=new Account(100);
Person p1=new Person(account,80);
Person p2=new Person(account,90);
p1.start();
p2.start();
}
}
class Account{
int total;
public Account(int total) {
super();
this.total=total;
}
}
class Person extends Thread{
private int reduce;
public Account account;
public Person(Account account,int reduce) {
this.account=account;
this.reduce=reduce;
}
public synchronized void run() {
if (account.total-reduce<0) return ;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
account.total-=reduce;
System.out.println(Thread.currentThread().getName()+"取出"+reduce);
System.out.println(Thread.currentThread().getName()+"剩余"+account.total);
}
}
Thread-0取出80
Thread-0剩余-70
Thread-1取出90
Thread-1剩余-70
这里是模拟的从账户取钱的操作,是用的同步方法,但是这里锁失败了。我看别人说是account没锁住,但是
account也是person对象的成员啊,同步方法不是把person对象锁住了吗,为什么这里没上锁呢?
精慕HU
不负相思意
HUWWW
随时随地看视频慕课网APP
相关分类