云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 第5章 多线程 - 补充案例

第5章 多线程 - 补充案例

  • 62 次阅读
  • 3 次下载
  • 2026/4/30 3:25:49

博学谷——让IT教学更简单,让IT学习更有效

}

// 生产者类

class Producter implements Runnable { }

// 消费者类

class Customer implements Runnable { }

public class Example10 { }

public static void main(String[] args) { }

Product p = new Product(); Producter pr = new Producter(p); Customer cr = new Customer(p); Thread t1 = new Thread(pr); Thread t2 = new Thread(pr); Thread t3 = new Thread(cr); Thread t4 = new Thread(cr); t1.start(); t2.start(); t3.start(); t4.start(); private Product p; Customer(Product p) { }

public void run() { }

while (true) { }

p.get(); this.p = p; private Product p; Producter(Product p) { }

public void run() { }

while (true) { }

p.set(\鼠标\this.p = p;

运行结果如图5-10所示。

17

博学谷——让IT教学更简单,让IT学习更有效

图5-10 运行结果

三、案例总结

1、wait()、notify()和notifyAll()这三个方法的调用者都应该是同步锁对象,如果这三个方法的调用者不是同步锁对象,Java虚拟机就会抛出IllegalMonitorStateException异常。

2、在线程通信中的,如果需要控制多个线程按照一定的顺序轮流执行,则这几个线程的锁对象必须是同一个锁对象。

18

搜索更多关于: 第5章 多线程 - 补充案例 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

博学谷——让IT教学更简单,让IT学习更有效 } // 生产者类 class Producter implements Runnable { } // 消费者类 class Customer implements Runnable { } public class Example10 { } public static void main(String[] args) { } Product p = new Product(); Producter pr = new Producter(p); Customer cr = new Customer(p); Thread t1 = new Th

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com