您的位置:首页 > 编程语言

有三个线程ID分别是ABC,用多线程编程在屏幕上循环打印ABCABC....十次

2010-10-13 00:05 411 查看



package thread;

/**

* 有三个线程ID分别是ABC,用多线程编程在屏幕上循环打印ABCABC....十次

* @author luoxiaoyi

*/

public class TestThread {

public static void main(String[] args) {

new Thread(new OrderThread(0,'A')).start();

new Thread(new OrderThread(1,'B')).start();

new Thread(new OrderThread(2,'C')).start();

}

}

class OrderThread implements Runnable {

//锁对象

private static Object o = new Object();

//用来记录是哪个线程进入运行

private static int count = 0;

//每个线程的标识,名称

private char ID;

//用来控制是线程运行的标识

private int id;

//每个线程运行的次数

private int num = 0;

public OrderThread(int id,char ID) {

this.id = id;

this.ID = ID;

}

public void run() {

synchronized (o) {

while (num < 10) {

if(count % 3 == id){

System.out.print(ID);

++ count;

++ num;

o.notifyAll();

}

else{

try {

o.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

}
本文出自 “风轻云淡” 博客,请务必保留此出处http://beyond99.blog.51cto.com/1469451/404900
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐