Code example for TimeUnit

Methods: sleep

0
 
    // 增加存款 
    public synchronized void addAmount(double amount) {
        double tmp = balance;
        try { 
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } 
        tmp += amount;
        this.balance = tmp;
    } 
 
    // 减少存款 
    public synchronized void subtractAmount(double amount) {
        double tmp = this.balance;
        try { 
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }