`
my_corner
  • 浏览: 83520 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

三个线程循环打印abc十次

阅读更多
朋友问的题,试着写写。也许有其他实现方式,感觉题目应该是考察线程间协作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();
						}
					}
				}
			}
		}
	}
	
}
分享到:
评论

相关推荐

    最新Java面试宝典pdf版

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    Java面试笔试资料大全

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    Java面试宝典2010版

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 64、说出ArrayList,Vector, LinkedList的存储性能和特性 65、去掉一个Vector集合中重复的元素 66、Collection 和 Collections的区别。 67、Set里的元素...

    Java面试宝典2012版

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67...

    Java面试宝典2012新版

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    Java面试宝典-经典

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    JAVA面试宝典2010

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    java面试题大全(2012版)

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    java面试宝典2012

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 49 64、说出ArrayList,Vector, LinkedList的存储性能和特性 50 65、去掉一个Vector集合中重复的元素 50 66、Collection 和 Collections的区别。 51 67、Set...

    正则表达式

    / //匹配三个单字字符和一个任意的数字. /\s+java\s+/ //匹配字符串"java" ,并且该串前后可以有一个或多个空格. /[^"] * / //匹配零个或多个非引号字符. 正则表达式的复制字符 字符 含义 ________________...

    Java 面试宝典

    4、在 JAVA 中如何跳出当前的多重嵌套循环? .......................................................... 8 5、switch 语句能否作用在 byte 上,能否作用在 long 上,能否作用在 String 上? .. 9 6、short s1 = ...

    软件工程-理论与实践(许家珆)习题答案

    (2) 模拟和分析需求 需求分析和模拟又包含三个层次的工作。首先是需求建模。需求模型的表现形式有自然语言、半形式化(如图、表、结构化英语等)和形式化表示等三种。需求概念模型的要求包括实现的独立性:不模拟数据...

Global site tag (gtag.js) - Google Analytics