import java.lang.Thread;public class MyThread extends Thread
{
int count= 1, number;
public MyThread(int num)
{
number = num;
System.out.println
("创建线程 " + number);
}
public void run() {
while(true) {
System.out.println
("线程 " + number + ":计数 " + count);
if(++count== 6) return;
}
}
public static void main(String args[])
{
MyThread thread = new MyThread(1);
thread.start();
try{
while(thread.isAlive())
Thread.sleep(5000);
}
catch(InterruptedException e){
}
thread.start();//?????
}
}上面的代码是一个多线程的例子,第二个start的时候会出错,一个线程对象不能启动多次吗?为什么呢?
{
int count= 1, number;
public MyThread(int num)
{
number = num;
System.out.println
("创建线程 " + number);
}
public void run() {
while(true) {
System.out.println
("线程 " + number + ":计数 " + count);
if(++count== 6) return;
}
}
public static void main(String args[])
{
MyThread thread = new MyThread(1);
thread.start();
try{
while(thread.isAlive())
Thread.sleep(5000);
}
catch(InterruptedException e){
}
thread.start();//?????
}
}上面的代码是一个多线程的例子,第二个start的时候会出错,一个线程对象不能启动多次吗?为什么呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货