java编写一个用于继承Thread并创建两个线程,每个线程输出从1到5的数
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 (注意大小写,别改文件名)
--------------------------有问题发消息我----------------------------
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 (注意大小写,别改文件名)
--------------------------有问题发消息我----------------------------
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询