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