这段代码:
class Bank
{
private int sum;
public synchronized void add (int n)
{
sum = sum +n;
try{wait(10);}catch (Exception e){}
System.out.println("sum = "+sum);
}
}
class Cus implements Runnable
{
Bank b = new Bank();
public void run()
{
for (int x = 0;x<3 ;x++)
{
b.add(100);
}
}
}
class BankDemo
{
public static void main(String[] args)
{
Cus c = new Cus();
Thread t1 = new Thread(c);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
我运行的结果一直是 200 300 400 500 600 600.难道是多核机的问题吗?毕老师视频的银行存钱练习讲解,求解释?作者: 梁耀今 时间: 2013-3-8 01:41 本帖最后由 梁耀今 于 2013-3-8 01:53 编辑