日期:2014-05-20 浏览次数:20644 次
public class th4_1{
public static void main(String[] args) throws InterruptedException {
cccc dog=new cccc("doggy");
cccc cat=new cccc("kitty");
Thread dog1 = new Thread(dog);
Thread cat1 = new Thread(cat);
synchronized(dog1){
try {
dog1.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cat1.start();
dog1.start();
dog1.notify();
}
}
}
public class cccc implements Runnable{
private String id;
public cccc(String str){
id=str;
}
public void run(){
for (int i = 0; i < 4; i++) {
try {
Thread.sleep((int)(1000*Math.random()));
}
catch (InterruptedException e) {
// TODO: handle exception
}
System.out.println(id+" is running");
}
}
}