这个java程序,为什么一个notify唤醒了3个wait
来源:互联网 发布:淘宝上的男内裤买家秀 编辑:程序博客网 时间:2024/06/16 21:07
1、
public class Demo6 {public static void main(String[] args) {Go1 q = new Go1();Go2 qq = new Go2(q);Go3 qqq = new Go3(q);Come w = new Come(q);q.start();qq.start();qqq.start();w.start();}}// 线程1,打印1,等待,唤醒后打印一class Go1 extends Thread {public void run() {synchronized (this) {System.out.println("1");try {wait();} catch (Exception e) {}}System.out.println("一");}}// 线程2,打印2,等待,唤醒后打印二class Go2 extends Thread {Go1 g;Go2(Go1 g) {this.g = g;}public void run() {synchronized (g) {System.out.println("2");try {g.wait();} catch (Exception e) {}}System.out.println("二");}}// 线程3,打印3,等待,唤醒后打印三class Go3 extends Thread {Go1 g;Go3(Go1 g) {this.g = g;}public void run() {synchronized (g) {System.out.println("3");try {g.wait();} catch (Exception e) {}}System.out.println("三");}}// 唤醒线程class Come extends Thread {Go1 r;Come(Go1 r) {this.r = r;}public void run() {try {sleep(100);} catch (Exception e) {}synchronized (r) {r.notify();System.out.println("lock open");}}}
结果:
1
2
3
lock open
一
三
二
答案:
原因是共用的对象本身也是一个线程,所以notify的时候,如果被唤醒的是Go1的线程,那么Go2和Go3中的g.wait();也会跟着返回,所以相当于Go1,Go2,Go3都被唤醒,然后一起争夺锁。把共用的对象换成一个普通的对象就没有问题了。代码如下:
public class Demo7 {public static void main(String[] args) {Go g = new Go();Go1 q = new Go1(g);Go2 qq = new Go2(g);Go3 qqq = new Go3(g);Come w = new Come(g);q.start();qq.start();qqq.start();w.start();}}class Go {}class Go1 extends Thread {Go g;Go1(Go g) {this.g = g;}public void run() {synchronized (g) {System.out.println("1");try {g.wait();} catch (Exception e) {}}System.out.println("一");}}class Go2 extends Thread {Go g;Go2(Go g) {this.g = g;}public void run() {synchronized (g) {System.out.println("2");try {g.wait();} catch (Exception e) {}}System.out.println("二");}}class Go3 extends Thread {Go g;Go3(Go g) {this.g = g;}public void run() {synchronized (g) {System.out.println("3");try {g.wait();} catch (Exception e) {}}System.out.println("三");}}class Come extends Thread {Go r;Come(Go r) {this.r = r;}public void run() {try {sleep(100);} catch (Exception e) {}synchronized (r) {r.notify();System.out.println("lock open");}}}
结果:
1
2
3
lock open
一
来源:javaeye问答
http://www.iteye.com/problems/89570
0 0
- 这个java程序,为什么一个notify唤醒了3个wait
- JAVA 线程等待唤醒,wait and notify
- java多线程notify()无法唤醒wait()问题
- Java 为什么使用wait()/notify()机制?
- Java-线程$等待唤醒机制(wait,notify)
- Java线程之等待wait唤醒notify示例(一)
- java多线程学习之线程通信-wait()等待 notify()唤醒
- wait() 不一定需要notify()唤醒
- 多线程wait等待和notify唤醒
- wait() 一定需要notify()唤醒吗?
- 用synchronized、notify、wait实现一个火车票卖票程序
- Java多线程3:wait、notify和notifyAll
- java,wait与notify
- Java多线程wait/notify
- java,wait与notify
- JAVA wait(), notify(),sleep
- java wait,notify使用
- Java Notify Wait Synchronized
- Linux常用命令(27)之源码包安装
- 使用第三方库出现找不到so库UnsatisfiedLinkError错误的原因以及解决方案
- Ehcache和MemCached比较分析
- web flow2 的helloworld
- 解决JSP中文乱码问题
- 这个java程序,为什么一个notify唤醒了3个wait
- Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion' has value '1.8', bu
- tomcat 启动报错
- Java环境搭建
- Android平台中进程与线程的基本知识
- 最简单的js异步查询功能
- superuser root 原理详细分析
- 访问JSP错误
- 使用VirtualBox创建Linux