public class Example10_6 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Clock clock =new Clock();
clock.xiaoming.start();
clock.clock1.start();
}
}
package xiancheng3;
public class Clock implements Runnable{
Thread xiaoming,clock1;
Clock(){
xiaoming=new Thread(this);
clock1=new Thread(this);
xiaoming.setName("小明");
clock1.setName("闹钟");
}
public void run(){
//String t=Thread.currentThread();
if(Thread.currentThread()==xiaoming){
try{
//System.out.println(Thread.currentThread());
System.out.println(xiaoming .getName()+"正在睡觉");
Thread.sleep(1000*60*60);
}
catch(InterruptedException e){
System.out.println(xiaoming.getName()+"被闹钟叫醒了");
}
}
else if(Thread.currentThread()==clock1){
for(int i=0;i<3;i++){
System.out.println("老公,起床了");
}
try{
Thread.sleep(500);
}
catch(InterruptedException e){}
}
xiaoming.interrupt();
}
}