java编写一个用于继承Thread并创建两个线程,每个线程输出从1到5的数

2024-11-17 04:37:28
推荐回答(1个)
回答1:

--------------------------napolun180410-----------------------------

public class MyThread extends Thread{
public void run(){
try{
for(int i=1;i<6;i++){
System.out.println(i);
sleep(2000);
}
}
catch(InterruptedException e){}
}
public static void main(String args[]){
System.out.println("当前的线程数:"+Thread.activeCount());
MyThread thread1=new MyThread();
thread1.start();
System.out.println("当前的线程数:"+Thread.activeCount());
MyThread thread2=new MyThread();
thread2.start();
System.out.println("当前的线程数:"+Thread.activeCount());
}
}

文件名 MyThread.java (注意大小写,别改文件名)

--------------------------有问题发消息我----------------------------