请大侠帮忙解决一个线程问题!小弟感激不尽!

zjfc_xth 2007-08-07 07:30:20
本人刚学java 不久,在验证多线程如何在同一程序中运行时,此程序不知哪里出了问题,请高手帮忙看看!谢谢你们得指点迷津!
class Sister extends Thread{
public Sister(String str){
super(str);

}
public void run(){
for(int i=0;i<6;i++)
{
System.out.println(getName()+"is running:"+i);
try{sleep((int)(Math.random()*1000));}
catch(InterruptedException e){}

}

}
System.out.println(getName()+"is running:"+i);
}





class CountingThread {
public static void main(String []args){
System.out.println("the siters Janny and Marry are counting the threads");
System.out.println("who are the first to runnig?");
new Sister("Janny").start();
new Sister("Marry").start();
}

}



...全文
232 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
liudongyan 2007-08-08
  • 打赏
  • 举报
回复

class Sister extends Thread {
public Sister(String str) {
super(str);

}

public void run() {
for (int i = 0; i < 6; i++) {
System.out.println(getName() + "is running:" + i);
try {
sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
}

System.out.println(getName() + "is running:" + i);
}

}
}

public class CountingThread {
public static void main(String[] args) {
System.out
.println("the siters Janny and Marry are counting the threads");
System.out.println("who are the first to runnig?");
new Sister("Janny").start();
new Sister("Marry").start();
}

}
ohuan 2007-08-08
  • 打赏
  • 举报
回复
楼主在开玩笑哦。。。在DOS里根本就编译不通过,出现下面错误!

CountingThread.java:16: <identifier> expected
System.out.println(getName()+"is running:"+i);
^
1 error


你的第二个System语句就没有放在方法内部!
qingtian_zhen 2007-08-08
  • 打赏
  • 举报
回复
是你工具没用明白
你把这个文件在DOS里面去运行试一下
我的都好使
dd379980649 2007-08-08
  • 打赏
  • 举报
回复
你写错了2个地方
第一个错:


}
System.out.println(getName()+"is running:"+i);
}
这里的这句代码不能放在类里面 只能放在类里的一个方法中.

第二个错:
class CountingThread {
public static void main(String []args){
包含 main方法的类,要放在一个声明为public而且和文件名相同的类当中.


以上两个地方就你的错误了~~
niueric 2007-08-08
  • 打赏
  • 举报
回复
(String []args) ????????????
jackshen016 2007-08-08
  • 打赏
  • 举报
回复
------------------------------------
ohuan(orckerth) ( ) 信誉:100
-------------------------------------
正解

还有保存的.java文件名要和包含main()方法的那个类的类名相同
clevercong 2007-08-07
  • 打赏
  • 举报
回复
class CountingThread 前面加个public就没有cant find main class问题了
zjfc_xth 2007-08-07
  • 打赏
  • 举报
回复
我不在DOS下编译,我用的是Eclipse
jingtao8177 2007-08-07
  • 打赏
  • 举报
回复
javac 文件名.java
java CountingThread
zjfc_xth 2007-08-07
  • 打赏
  • 举报
回复
可是编译得时候,出错信息是cant find main class 怎么回事呢?
zjy0009 2007-08-07
  • 打赏
  • 举报
回复
"System.out.println(getName()+"is running:"+i);"怎么是个孤儿啊,而且这个i从那里读的到啊??
jingtao8177 2007-08-07
  • 打赏
  • 举报
回复
System.out.println(getName()+"is running:"+i);
==============================================
没在方法里边吧

62,623

社区成员

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

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