问题描述
- 新手求助关于Java多线程中启动线程问题
- public class Example15_1 {
/** * @param args */public static void main(String[] args) { // TODO Auto-generated method stub SpeakHello speakHello; SpeakNinhao speakNinhao; speakHello = new SpeakHello(); speakNinhao= new SpeakNinhao(); speakHello.start(); speakNinhao.start(); for(int i=1;i<=5;i++){ System.out.println(""大家好""+i+""""); } }
}
public class SpeakHello extends Thread {
public void run(){
for(int i=1;i<=7;i++){
System.out.println(""hello""+i+"""");
}
}
}
public class SpeakNinhao {
public void run(){
for(int i=1;i<=7;i++){
System.out.println(""ninhao""+i+"""");
}
}
}
speakNinhao.start();这个语句Myeclipse里面报错:The method start()is undefined for the type 'SpeakNinhao'.我发现即使在SpeakNinhao类里面写一个start()也不能通过编译。谁知道如何改
解决方案
SpeakNinhao类没有继承Thread类
解决方案二:
与一楼见解一致,需要继承thread类或实现runnable接口
时间: 2024-09-11 20:52:27