QTP学习记录(三)
2008-12-04 17:38
183 查看
朋友问的题,试着写写。也许有其他实现方式,感觉题目应该是考察线程间协作wait和notify所以选择如下方式实现:
/** * @author my_corner * 2011-12-26 */ public class ThreadPrint { /** * @author my_corner * @param * @return * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { PrintTask task = new PrintTask(); Thread a = new Thread(task); a.setName("a"); Thread b = new Thread(task); b.setName("b"); Thread c = new Thread(task); c.setName("c"); a.start(); b.start(); c.start(); } } class PrintTask implements Runnable{ private int times = 0; /** * */ @Override public void run() { while(times<30){ synchronized (this) { if(times%3==0){ if("a".equals(Thread.currentThread().getName())){ System.out.print("a"); times++; this.notifyAll(); }else{ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } if(times%3==1){ if("b".equals(Thread.currentThread().getName())){ System.out.print("b"); times++; this.notifyAll(); }else{ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } if(times%3==2){ if("c".equals(Thread.currentThread().getName())){ System.out.print("c"); times++; this.notifyAll(); }else{ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } }
相关文章推荐
- 001安卓AIDL学习记录
- HTML5学习记录二:画布
- CSS学习记录1
- Windows学习记录之网络编程sockaddr_in结构体
- C#学习记录(七)--------------AD操作
- FFmpeg学习问题集合记录
- 自动化_零基础学习QTP视频教程
- 《Linux Shell 脚本攻略》学习记录之二
- Swift 学习记录(2)
- 在别的地方看的<<给程序员介绍一些C++开源库>>,记录给大家共同学习
- java学习记录【day14】
- Massive Data Mining学习记录
- 使用JQuery进行WCF通信的学习记录
- 思维导图, 你我工作、学习、记录的好工具, 一个字:实用
- c学习记录
- 【记录学习linux】第一天
- 我的学习记录173
- JAVA API 学习疑问记录
- 学习记录
- XCode中创建第一个类 -记录学习过程