采用Java多线程技术编写程序, 100分求解

来源:百度知道 编辑:UC知道 时间:2024/05/23 14:11:54
采用Java多线程技术编写程序,其中包括两个线程:A和B,其中A线程准备休眠一小时,B线程每隔一秒输入3句“起床”后,吵醒休眠的线程A。

public class T2 {

public final static Thread t1 = new SleepThread();
public static void main(String[] args) {
Thread t2 = new Thread("wake_up") {
public void run() {
while(true) {
try {
Thread.sleep(1000);
for (int i=0; i<3; i++)
System.out.println("起床");
synchronized(t1) {
t1.interrupt();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t1.start();
t2.start();

}
}
class SleepThread extends Thread
{
public void run()
{
while(true) {
try {
Thread.sleep(1000*60*60);
} catch (InterruptedException e) {
System.out.println("吵醒休眠线程A");
//do what you want after the thread are waked up
}
}
}
}

?