关于java线程的问题

Vanquish_self 2014-09-02 09:37:19
package BOOK;

public class ThreadTerminate {
public static void manin(String args[])throws Exception{
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;
}
}

这是java书上关于一段线程的代码
为什么我输入到电脑里可是却无法运行
求大神解答
...全文
232 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sui2121 2014-09-05
  • 打赏
  • 举报
回复
我就呵呵了 你的工具不报错吗? 难道你是床说中用记事本写程序的大神?
Star-小东 2014-09-05
  • 打赏
  • 举报
回复
这哪是线程问题。。。。。。。这是有关线程代码的书写问题!
一大三千 2014-09-05
  • 打赏
  • 举报
回复
Vanquish_self 2014-09-04
  • 打赏
  • 举报
回复
一不小心逗了。。。
sca4441479 2014-09-02
  • 打赏
  • 举报
回复
这。。。。。 害我看一段代码看半天。。
shixitong 2014-09-02
  • 打赏
  • 举报
回复
public static void manin(String args[]) 改为 public static void main(String args[])
qzw1210 2014-09-02
  • 打赏
  • 举报
回复

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;
}
}

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧