JAVA#线程练习'札记

 public static void main(String[] args) {
        Account account=new Account();
        customer customer1=new customer(account);
        customer customer2=new customer(account);
        customer1.setName("宋小艾");
        customer2.setName("宋科比");
        customer1.start();
        customer2.start();

    }
}
class customer extends Thread{
    Account account;
    public customer(Account account){
        this.account=account;
    }
    public synchronized void run(){//声明为synchronized
        for(int i=0;i<3;i++){
            account.deposit(1000.00);
        }
    }
}
class  Account{
    double balance;
    public Account(){
    }
    public void deposit(double amount){
        balance-=amount;
        System.out.println(Thread.currentThread().getName()+":"+balance);
    }
}
宋小艾:-1000.0
宋科比:-2000.0
宋小艾:-3000.0
宋科比:-4000.0
宋小艾:-5000.0
宋科比:-6000.0

猜你喜欢

转载自blog.csdn.net/Iverson941112/article/details/85719381
今日推荐