採用Java多線程技術編寫程序,其中包括兩個線程:A和B,其中A線程準備休眠一小時,B線程每隔一秒輸入3句“起牀”後,吵醒休眠的線程A。
public class T2{
public static final SleepThread A=new SleepThread();
public static void main(String[] args) {
Thread B=new Thread("Wake_up"){
public void run(){
while(true){
try {
sleep(1000);
for(int i=0;i<3;i++){
System.out.println("起牀");
}
synchronized(A){
A.interrupt();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
A.start();
B.start();
}
}
class SleepThread extends Thread{
public void run(){
while(true){
try {
sleep(1000*60*60);
} catch (Exception e) {
// TODO: handleexception
System.out.println("吵醒休眠線程A");
}
}
}
}