朋友问的题,试着写写。也许有其他实现方式,感觉题目应该是考察线程间协作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();
}
}
}
}
}
}
}
分享到:
相关推荐
63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...
63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...
63、List、Map、Set三个接口,存取元素时,各有什么特点? 64、说出ArrayList,Vector, LinkedList的存储性能和特性 65、去掉一个Vector集合中重复的元素 66、Collection 和 Collections的区别。 67、Set里的元素...
63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67...
63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...
63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...
63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...
63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...
63、List、Map、Set三个接口,存取元素时,各有什么特点? 49 64、说出ArrayList,Vector, LinkedList的存储性能和特性 50 65、去掉一个Vector集合中重复的元素 50 66、Collection 和 Collections的区别。 51 67、Set...
/ //匹配三个单字字符和一个任意的数字. /\s+java\s+/ //匹配字符串"java" ,并且该串前后可以有一个或多个空格. /[^"] * / //匹配零个或多个非引号字符. 正则表达式的复制字符 字符 含义 ________________...
4、在 JAVA 中如何跳出当前的多重嵌套循环? .......................................................... 8 5、switch 语句能否作用在 byte 上,能否作用在 long 上,能否作用在 String 上? .. 9 6、short s1 = ...
(2) 模拟和分析需求 需求分析和模拟又包含三个层次的工作。首先是需求建模。需求模型的表现形式有自然语言、半形式化(如图、表、结构化英语等)和形式化表示等三种。需求概念模型的要求包括实现的独立性:不模拟数据...