传统线程互斥技术----实现
2013-11-05 21:24
330 查看
//子线程循环10次,接着主线程循环100次,接着在子线程循环10次,接着再主线程循环100次 public class SynTest { public static void main(String[] args) { final Business b = new Business(); new Thread(new Runnable() { public void run() { for (int i = 1; i <= 50; i++) { b.sub(); } } }).start(); new Thread(new Runnable() { public void run() { for (int i = 1; i <= 50; i++) { b.main(); } } }).start(); } } class Business { private boolean flag = true; public synchronized void sub() { while (!flag) {// 用while防止假唤醒 try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int j = 1; j <= 10; j++) { System.out.println("sub thread:" + j); } flag = false; this.notify(); } public synchronized void main() { while (flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int j = 1; j <= 100; j++) { System.out.println("main thread:" + j); } flag = true; this.notify(); } }
相关文章推荐
- gmail,Qmail,163邮箱的IMAP/SMTP/POP3地址及协议端口
- 程序员编程技术迅速提高终极攻略
- Qt程序对文本文件的读写
- 世界上最简单的无锁哈希表
- 黑马程序员_乱码问题
- Android代号
- Struts2注解Convention扫描jar中的Action的设置,注解Action咋jar中找不到.
- EBS form日历可选范围设置(calendar.setup )介绍
- 黄聪:C#多线程教程(1):BeginInvoke和EndInvoke方法,解决主线程延时Thread.sleep柱塞问题(转)
- XML基础
- uva 10616 - Divisible Group Sums(计数)
- UVA 11121 - Base -2 (数论)
- Yii框架tips--经常实用的东西
- hdu_1048_The Hardest Problem Ever_201311052052
- SCEP - Simple Certificate Enrollment Protocol 用于MDM服务器的设备证书颁发
- 线性代数导论29——相似矩阵和若尔当形
- Java基础1--概述--环境变量--HelloWorld
- oracle批量update和delete
- 黑马程序员_多线程
- Web.Config的配置