本人刚学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();
}
}
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();
}
}
解决方案 »
- static 问题
- 求助:java正则表达式解析find kw=a kw1=b kw3=d c
- 为什么一直提示找不到Double.parseDouble?谁能帮我把它运行出来?
- 事件处理的问题,和线程问题,帮帮忙哈
- 真是又简单又奇怪呀
- 请问 String host 是(如 www.sina.com.cn )吗 有192.9.200.250 怎么产生 intAddress
- 求助这是什么问题?
- 怎么在jpanel中获得当前的jframe对象
- JBuild 6的IDE环境问题
- JAVA课程设计,紧急求助北京的朋友,在线等!给分又 加 报酬!
- jdbc 纯JAVA代码数据库连接...
- 一个不明不败的错误!新手,不知道哪里的问题。望高手解决下,谢谢了!
==============================================
没在方法里边吧
java CountingThread
你把这个文件在DOS里面去运行试一下
我的都好使
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()方法的那个类的类名相同
第一个错:
}
System.out.println(getName()+"is running:"+i);
}
这里的这句代码不能放在类里面 只能放在类里的一个方法中.第二个错:
class CountingThread {
public static void main(String []args){
包含 main方法的类,要放在一个声明为public而且和文件名相同的类当中.
以上两个地方就你的错误了~~