Java 为什么不调用wait方法后面的代码
我有一个customer account类,包含以下两种方法:addBalance和DecluttBalanceJava 为什么不调用wait方法后面的代码,java,java-threads,thread-synchronization,Java,Java Threads,Thread Synchronization,我有一个customer account类,包含以下两种方法:addBalance和DecluttBalance class CustomerAccount { private int balance; int getBalance() { return balance;} CustomerAccount() {balance=0;} boolean deductBalance(final int amount) { System
class CustomerAccount
{
private int balance;
int getBalance() { return balance;}
CustomerAccount() {balance=0;}
boolean deductBalance(final int amount)
{
System.out.println("> invoked to deduct :" + amount);
synchronized (this)
{
while (balance <= amount)
{
try {wait(); } catch (InterruptedException e) {TestWaitNotifyBasic.logException(e);}
System.out.println("> hey I was notified for cutting amount:" + amount);
}
balance-= amount;
}
System.out.println("> deducted:" + amount);
return true;
}
boolean addBalance(final int amount)
{
synchronized (this)
{
balance += amount;
notifyAll();
}
System.out.println("> balance added: " + amount);
return true;
}
}
我的问题是,为什么控制台输出中不打印下面的内容
> balance added: 80
> invoked to deduct :1000
> balance added: 8000
> invoked to deduct :5000
> invoked to deduct :50
> balance added: 900
> balance added: 40
> deducted:5000
> deducted:50
> deducted:1000
----------------------
thread operations finished, final balance : 2970
Validity check 2970
System.out.println(“>嘿,我收到了削减金额的通知:“+amount”)
此方法写在CustomerAccount类中Decrete balance方法的wait()旁边。在注释之后,在进入执行之前将addBalance线程修改为睡眠。在这之后,所有的系统输出都被执行了
上面发布的代码的行为是addBalance线程将在deductBalance线程进入同步块之前执行
使用下面的addBalance方法,ouptut会发生变化
boolean addBalance(final int amount)
{
try {Thread.sleep(3000);} catch (InterruptedException e) {{TestWaitNotifyBasic.logException(e);}}
synchronized (this)
{
balance += amount;
notifyAll();
}
System.out.println("> balance added: " + amount);
return true;
}
控制台输出post方法更改:
> invoked to deduct :50
> invoked to deduct :1000
> invoked to deduct :5000
> balance added: 900
> balance added: 40
> hey I was notified for cutting amount:50
> balance added: 8000
> balance added: 80
> hey I was notified for cutting amount:5000
> deducted:50
> hey I was notified for cutting amount:1000
> deducted:1000
> deducted:5000
----------------------
thread operations finished, final balance : 2970
Validity check 2970
永远不要写
catch(InterruptedException e){}
。至少记录exception@Jens,这是练习用的。。不是生产代码。这不是让catch块为空的原因。您将永远看不到是否存在可以更改程序流的异常。可能的原因是,在“add 8000”之前,没有任何演绎线程进入同步块。因此,他们从未进入而循环。@Jens添加了异常日志记录。原始代码中没有问题,我只是想查看控制台输出。问题是“为什么控制台输出不可见”?这就是我的意思。当我运行您的原始代码时,我得到了您的预期输出,即,>嘿,当我运行您的原始代码时,控制台中确实出现了削减金额的通知。
> invoked to deduct :50
> invoked to deduct :1000
> invoked to deduct :5000
> balance added: 900
> balance added: 40
> hey I was notified for cutting amount:50
> balance added: 8000
> balance added: 80
> hey I was notified for cutting amount:5000
> deducted:50
> hey I was notified for cutting amount:1000
> deducted:1000
> deducted:5000
----------------------
thread operations finished, final balance : 2970
Validity check 2970