Java 将wait、Signal、SignalAll替换为wait、notify和notifyAll
我想用wait()、notifyAll()和下面列出的代码替换wait()、signalAll(),问题是一旦线程进入等待状态。它再也不会被通知了。。谢谢你的帮助Java 将wait、Signal、SignalAll替换为wait、notify和notifyAll,java,multithreading,java.util.concurrent,Java,Multithreading,Java.util.concurrent,我想用wait()、notifyAll()和下面列出的代码替换wait()、signalAll(),问题是一旦线程进入等待状态。它再也不会被通知了。。谢谢你的帮助 import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.Condition; import java.ut
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class AccountThreadCommunicationDefault {
private static Account account = new Account();
private static Random random = new Random();
public static void main(String... args) {
ExecutorService es = Executors.newFixedThreadPool(2);
es.execute(new WithdrawTask());
es.execute(new DepositTask());
es.shutdown();
}
private static class WithdrawTask implements Runnable {
public void run() {
while (true) {
account.withdraw(random.nextInt(600));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static class DepositTask implements Runnable {
public void run() {
while (true) {
account.deposit(random.nextInt(500));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static class Account {
private Integer balance = 500;
private static Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
// private Object object = new Object();
// public synchronized void deposit(int count) {
public void deposit(int count) {
try {
lock.lock();
System.out.print("当前账户余额:" + balance);
balance += count;
System.out.println(" 存款:" + count + " 账户余额(deposit): "
+ getBalance());
// synchronized (object) {
// object.notifyAll();
// }
synchronized (this) {
this.notify();
}
} catch (IllegalMonitorStateException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void withdraw(int count) {
try {
lock.lock();
while (balance < count) {
System.out.println("余额不足:" + getBalance() + " 等待存款..");
// synchronized (object) {
// object.wait();
// }
synchronized (this) {
this.wait();
}
System.out.print("当前账户余额:" + balance);
balance -= count;
System.out.println(" 取现:" + count + " 账户余额(withdraw): "
+ getBalance());
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public int getBalance() {
return this.balance;
}
}
}
import java.util.Random;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.locks.Condition;
导入java.util.concurrent.locks.Lock;
导入java.util.concurrent.locks.ReentrantLock;
公共类AccountThreadCommunicationDefault{
私有静态帐户=新帐户();
私有静态随机=新随机();
公共静态void main(字符串…参数){
Executors服务es=Executors.newFixedThreadPool(2);
执行(新任务());
执行(新任务());
es.shutdown();
}
私有静态类任务实现可运行{
公开募捐{
while(true){
账户取款(随机取款(600));
试一试{
睡眠(5000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
}
私有静态类DepositTask实现可运行{
公开募捐{
while(true){
账户。存款(随机。nextInt(500));
试一试{
睡眠(5000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
}
私有静态类帐户{
私人整数余额=500;
private static Lock=new ReentrantLock();
Condition Condition=lock.newCondition();
//私有对象=新对象();
//公共同步无效存款(整数计数){
公共无效存款(整数计数){
试一试{
lock.lock();
系统输出打印(“当前账户余额:" + 平衡);
余额+=计数;
System.out.println(“存款:“+计数+”账户余额(押金):”
+getBalance());
//已同步(对象){
//object.notifyAll();
// }
已同步(此){
this.notify();
}
}捕获(非法监视器状态异常e){
e、 printStackTrace();
}最后{
lock.unlock();
}
}
公共作废撤销(整数计数){
试一试{
lock.lock();
while(余额<计数){
System.out.println(“余额不足:“+getBalance()+”等待存款..");
//已同步(对象){
//object.wait();
// }
已同步(此){
这个。等等();
}
系统输出打印(“当前账户余额:“+余额);
余额-=计数;
System.out.println(“取现:“+计数+”账户余额(撤回):”
+getBalance());
}
}捕捉(中断异常e){
e、 printStackTrace();
}最后{
lock.unlock();
}
}
公共int getBalance(){
还这个。结余;
}
}
}
您遇到死锁。取款任务正在帐户对象的监视器上等待,同时保持可重入锁定。存款任务无法通知()帐户对象监视器,因为它无法锁定可重入锁定
您需要wait()和notify()作为互斥对象使用的同一个对象。不管它是ReentrantLock还是Account对象的监视器。但它不会像您用两个不同的锁编写它那样工作。为什么要将
wait()
和signalAll()
替换为wait()
notifyAll()?这毫无意义。只是为了更深入地研究java线程:)