public class Account {
private String name;
private double balance; public Account(String name) {
this.name = name;
this.balance = 0;
} public String getName() {
return name;
} public double balance() {
return balance; } public void put(double value) {
if (value > 0)
this.balance += value;
} public double get(double value) {
if (value > 0) {
if (value <= this.balance)
this.balance -= value;
else {
value = this.balance;
this.balance = 0;
}
return value;
}
return 0;
}

public static void main(String args[]) {
Account wang = new Account("wang");
(new Save(wang, 60)).start();
(new Save(wang, 80)).start();
(new Fetch(wang, 120)).start();
}
}class Save extends Thread {
private Account account;
private double value; public Save(Account a1, double value) {
this.account = a1;
this.value = value;
} public void run() {
double howmatch = this.account.balance();
this.account.put(this.value);
try {
sleep(1);
} catch (InterruptedException e) {
}
;
System.out.println(this.account.getName() + "帐户:  现有" + howmatch
+ ", 存入" + this.value + ", 余额" + this.account.balance()); }
}class Fetch extends Thread {
private Account account;
private double value; public Fetch(Account a1, double value) {
this.account = a1;
this.value = value;
} public void run() {
double howmatch = this.account.balance();

try {
sleep(1);
} catch (InterruptedException e) {
}
;
System.out.println(this.account.getName() + "帐户:  现有" + howmatch
+ ", 取出" + this.account.get(this.value) + ", 余额" + this.account.balance());
}
}帮忙分析下,线程的问题概念懂了,真让我分析各种结果 还真不大会,顺便说下,为什么余额 结果不会是0(即为什么第3个构造不能先运行完,我试了好多次了..)

解决方案 »

  1.   

    再比如我改成同步的  为什么结果会是 1/3/2 的顺序运行,有什么规律吗?public class Account {
    private String name;
    private double balance; public Account(String name) {
    this.name = name;
    this.balance = 0;
    } public String getName() {
    return name;
    } public double balance() {
    return balance; } public void put(double value) {
    if (value > 0)
    this.balance += value;
    } public double get(double value) {
    if (value > 0) {
    if (value <= this.balance)
    this.balance -= value;
    else {
    value = this.balance;
    this.balance = 0;
    }
    return value;
    }
    return 0;
    } public static void main(String args[]) {
    Account wang = new Account("wang");
    (new Save(wang, 60)).start();
    (new Save(wang, 80)).start();
    (new Fetch(wang, 120)).start();
    }
    }class Save extends Thread {
    private Account account;
    private double value; public Save(Account a1, double value) {
    this.account = a1;
    this.value = value;
    } public void run() {
    synchronized (this.account) {
    double howmatch = this.account.balance();
    this.account.put(this.value);
    try {
    sleep(1);
    } catch (InterruptedException e) {
    }
    ;
    System.out.println(this.account.getName() + "帐户:  现有" + howmatch
    + ", 存入" + this.value + ", 余额" + this.account.balance());
    }
    }
    }class Fetch extends Thread {
    private Account account;
    private double value; public Fetch(Account a1, double value) {
    this.account = a1;
    this.value = value;
    } public void run() {
    synchronized (this.account) {
    double howmatch = this.account.balance(); try {
    sleep(1);
    } catch (InterruptedException e) {
    }
    ;
    System.out.println(this.account.getName() + "帐户:  现有" + howmatch
    + ", 取出" + this.account.get(this.value) + ", 余额"
    + this.account.balance());
    }
    }
    }
      

  2.   

    程序看上去没什么问题,加了synchronized之后
      

  3.   

    这种问题就是加锁了,不是好懂的话就把锁的粒度放大点了(就是多加几把锁了),

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package 聊天;/**
     *
     * @author Administrator
     */
    public class Account {
        private String name;
        private double balance;    public Account(String name) {
            this.name = name;
            this.balance = 0;
        }    public String getName() {
            return name;
        }    public synchronized double balance() {
            return balance;    }    public synchronized void put(double value) {
            if (value > 0)
                this.balance += value;
        }    public synchronized double get(double value) {
            if (value > 0) {
                if (value <= this.balance)
                    this.balance -= value;
                else {
                    value = this.balance;
                    this.balance = 0;
                }
                return value;
            }
            return 0;
        }    public static void main(String args[]) {
            Account wang = new Account("wang");
            (new Save(wang, 60)).start();
            try {
                Thread.sleep(1000);
            } catch(Exception e) {        }
            (new Save(wang, 80)).start();
            try {
                Thread.sleep(1000);
            } catch(Exception e) {        }
            (new Fetch(wang, 120)).start();
            try {
                Thread.sleep(1000);
            } catch(Exception e) {        }
            (new Save(wang,1003)).start();
        }
    }class Save extends Thread {
        private Account account;
        private double value;    public Save(Account a1, double value) {
            this.account = a1;
            this.value = value;
        }    public void run() {
            double howmatch = this.account.balance();
            this.account.put(this.value);
            try {
                sleep(1000);
            } catch (InterruptedException e) {
            }
            System.out.println(this.account.getName() + "帐户:  现有" + howmatch
                    + ", 存入" + this.value + ", 余额" + this.account.balance());    }
    }class Fetch extends Thread {
        private Account account;
        private double value;    public Fetch(Account a1, double value) {
            this.account = a1;
            this.value = value;
        }    public void run() {
            double howmatch = this.account.balance();        try {
                sleep(1000);
            } catch (InterruptedException e) {
            }
            System.out.println(this.account.getName() + "帐户:  现有" + howmatch
                    + ", 取出" + this.account.get(this.value) + ", 余额" + this.account.balance());
        }
    }
    这样不就可以看出效果了吗?