为什么不调用等待方法之后的代码
Why is the code just after wait method not invoked
我有一个客户帐户 class,具有以下 2 种方法,addBalance 和 deductBalance。
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;
}
}
下面是main方法的消耗class。这个想法是让线程等待同步块,直到调用 notifyAll。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.IntFunction;
public class TestWaitNotifyBasic
{
public static void main(String[] args)
{
final CustomerAccount chuck = new CustomerAccount();
IntFunction<Runnable> addBalance = (amount) -> { return () -> chuck.addBalance(amount);};
IntFunction<Runnable> deductBalance = (amount) -> { return () -> chuck.deductBalance(amount);};
ExecutorService threadPool = Executors.newFixedThreadPool(100);
//balance deduction
threadPool.execute(deductBalance.apply(1000) );
threadPool.execute(deductBalance.apply(50) );
threadPool.execute(deductBalance.apply(5000));
//balance addition
threadPool.execute(addBalance.apply(900));
threadPool.execute(addBalance.apply(40));
threadPool.execute(addBalance.apply(80));
threadPool.execute(addBalance.apply(8000));
threadPool.shutdown();
while (!threadPool.isTerminated())
{
try {Thread.sleep(300);} catch (InterruptedException e) {{logException(e);}}
}
System.out.println("----------------------" );
System.out.println("thread operations finished, final balance : " + chuck.getBalance());
int actualBalance = (900+40+80+8000)-(1000+50+5000);
System.out.println("Validity check " + actualBalance);
}
static void logException(InterruptedException e)
{
System.out.println("###########interruptedexception#########" + e);
}
}
下面是我的控制台输出
> 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("> hey I was notified for cutting amount:" + amount);
此方法写在 CustomerAccount 中扣除余额方法的 wait() 旁边 class。
根据 @RealSkeptic's 评论,将 addBalance 线程修改为在进入执行前休眠。 Post 这所有的系统输出都被执行了
上面代码 post 的行为是 addBalance 线程将在 deductBalance 线程进入同步块之前执行。
使用以下 addBalance 方法,输出发生变化。
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
我有一个客户帐户 class,具有以下 2 种方法,addBalance 和 deductBalance。
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;
}
}
下面是main方法的消耗class。这个想法是让线程等待同步块,直到调用 notifyAll。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.IntFunction;
public class TestWaitNotifyBasic
{
public static void main(String[] args)
{
final CustomerAccount chuck = new CustomerAccount();
IntFunction<Runnable> addBalance = (amount) -> { return () -> chuck.addBalance(amount);};
IntFunction<Runnable> deductBalance = (amount) -> { return () -> chuck.deductBalance(amount);};
ExecutorService threadPool = Executors.newFixedThreadPool(100);
//balance deduction
threadPool.execute(deductBalance.apply(1000) );
threadPool.execute(deductBalance.apply(50) );
threadPool.execute(deductBalance.apply(5000));
//balance addition
threadPool.execute(addBalance.apply(900));
threadPool.execute(addBalance.apply(40));
threadPool.execute(addBalance.apply(80));
threadPool.execute(addBalance.apply(8000));
threadPool.shutdown();
while (!threadPool.isTerminated())
{
try {Thread.sleep(300);} catch (InterruptedException e) {{logException(e);}}
}
System.out.println("----------------------" );
System.out.println("thread operations finished, final balance : " + chuck.getBalance());
int actualBalance = (900+40+80+8000)-(1000+50+5000);
System.out.println("Validity check " + actualBalance);
}
static void logException(InterruptedException e)
{
System.out.println("###########interruptedexception#########" + e);
}
}
下面是我的控制台输出
> 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("> hey I was notified for cutting amount:" + amount);
此方法写在 CustomerAccount 中扣除余额方法的 wait() 旁边 class。
根据 @RealSkeptic's 评论,将 addBalance 线程修改为在进入执行前休眠。 Post 这所有的系统输出都被执行了
上面代码 post 的行为是 addBalance 线程将在 deductBalance 线程进入同步块之前执行。
使用以下 addBalance 方法,输出发生变化。
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