有关java多线程中notifyAll()方法的问题

JimmyU1 2016-11-24 04:24:43

下面是一个有关线程同步问题,notifyAll方法是解除等待线程的阻塞状态,请问里面的阻塞线程是指那些线程?
是请求访问这个方法临界资源的线程吗?
public synchronized void transfer(int from, int to, double amount) throws InterruptedException {
while(accounts[from] < amount)
wait();
System.out.print(Thread.currentThread());
accounts[from] -= amount;
System.out.printf("10.2%f from %d to %d",amount,from,to);
accounts[to] += amount;
System.out.printf("Total Balance: %10.2f\n",getTotalBalance());
notifyAll();
}

谢谢各位指点迷津
...全文
218 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
清泉流 2016-11-25
  • 打赏
  • 举报
回复
notifyall()方法属于对象,所以是会通知所有访问该对象的线程,最后谁先获得的对象的锁,谁就执行,其他的线程会继续阻塞
gw6328 2016-11-25
  • 打赏
  • 举报
回复
你这个写法就是this这个对象锁对应的请求线程。
soton_dolphin 2016-11-24
  • 打赏
  • 举报
回复
假设你的transfer 方法是在account 类里面。 Account acc = new Account(). acc 是Account 的一个实例,它拥有自己的一个锁。每一个要调用acc.transfer 方法的线程必须要检查这个锁是不是可用。如果线程1得到这个锁并执行transfer, 那么线程2就必须等待线程1执行完transfer之后,才能够得到acc的锁。那么线程2就是被阻塞线程

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧