日期:2014-05-20  浏览次数:20631 次

请大侠帮忙解决一个线程问题!小弟感激不尽!
本人刚学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();
}

}





------解决方案--------------------
System.out.println(getName()+ "is running: "+i);
==============================================
没在方法里边吧
------解决方案--------------------
"System.out.println(getName()+ "is running: "+i); "怎么是个孤儿啊,而且这个i从那里读的到啊??
------解决方案--------------------
class CountingThread 前面加个public就没有cant find main class问题了
------解决方案--------------------
是你工具没用明白
你把这个文件在DOS里面去运行试一下
我的都好使
------解决方案--------------------
楼主在开玩笑哦。。。在DOS里根本就编译不通过,出现下面错误!

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


你的第二个System语句就没有放在方法内部!

------解决方案--------------------

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(orckerth) ( ) 信誉:100
-------------------------------------
正解

还有保存的.java文件名要和包含main()方法的那个类的类名相同
------解决方案--------------------
(String []args) ????????????
------解决方案--------------------
你写错了2个地方
第一个错:


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