62,616
社区成员
发帖
与我相关
我的任务
分享
package demo.spli;
public class ThreadTerminate {
public static void main(String args[])throws Exception{//楼主把main方法写错了
int i=0;
Hello h=new Hello();
Thread t =new Thread(h);
t.setPriority(Thread.MAX_PRIORITY);
t.start();
System.out.println("Please stop");
h.stopRunning();
while(i<5){
System.out.println("Good Morning"+i++);
}
}
}
class Hello implements Runnable{
int i=0;
private boolean timeToQuit=false;
public void run(){
while(!timeToQuit){
System.out.println("Holle"+i++);
try{
if(i%2==0)
Thread.sleep(10);
}catch(Exception e){}
}
}
public void stopRunning(){
timeToQuit=true;
}
}