急急急,JAVA题目

来源:百度知道 编辑:UC知道 时间:2024/05/31 13:05:57
题目:主线程控制新线程的生命,当主线程运行一段时间后,控制新线程死亡,主线程继续运行一段时间后结束。

急需答案哦。在线等!麻烦高手帮帮忙咯!

class Test
{
public static void main (String[] args) throws Exception{
MyThread me=new MyThread();
me.start();
for(int i=0;i<10;i++)
{
Thread.sleep(1000);
System.out.println ("This is main Thread.");
}
me.setBoolean(false);
for(int i=0;i<5;i++)
{
Thread.sleep(1000);
System.out.println ("This is main Thread.");
}
System.out.println ("MainThread is end.");
}

}
class MyThread extends Thread
{
boolean b=true;
public void setBoolean(boolean b)
{
this.b=b;
}
public void run(){
for(;b==true;)
{
System.out.println ("This is MyThread");
try{
sleep(1000);
}
catch(Exception e){
}
}
System.out.println ("MyThread is end.");

}

}