继承Thread类使子类有多线程的能力
1.子类需要重写run()方法
package 第一种;
public class Thunder extends Thread{
@Override
public void run() {
//继承Thread需要重写run()方法
// TODO Auto-generated method stub
for(int i=1;i<101;i++) {
System.out.println(Thread.currentThread().getName()+"下载进度:"+i+"%");//使用这种只能是默认的名字或者在a.star()之前设置名字
try {
Thread.sleep(50);//没个子线程的休眠时间及时间片
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
测试类
package 第一种;
public class ThunderTest {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.out.println("我是主线程");
Thread s=Thread.currentThread();
boolean sk=s.isDaemon();//判断主函数是否是守护程序
System.out.println(sk);
Thunder a=new Thunder();
Thunder a1=new Thunder();
Thunder a2=new Thunder();
Thunder a3=new Thunder();
a.setName("复仇者联盟");
a.start();
a1.setName("复仇者联盟2");
a1.start();
a2.setName("复仇者联盟3");
a2.start();
a3.setName("复仇者联盟4");
a3.start();
}
/*
复仇者联盟2下载进度:1%
.。。。。
。。
复仇者联盟4下载进度:92%
复仇者联盟下载进度:92%
复仇者联盟2下载进度:93%
复仇者联盟3下载进度:93%
复仇者联盟4下载进度:93%
复仇者联盟下载进度:93%
复仇者联盟3下载进度:94%
复仇者联盟2下载进度:94%
复仇者联盟4下载进度:94%
复仇者联盟下载进度:94%
复仇者联盟2下载进度:95%
复仇者联盟3下载进度:95%
复仇者联盟4下载进度:95%
复仇者联盟下载进度:95%
复仇者联盟3下载进度:96%
复仇者联盟2下载进度:96%
复仇者联盟4下载进度:96%
复仇者联盟下载进度:96%
复仇者联盟2下载进度:97%
复仇者联盟3下载进度:97%
复仇者联盟下载进度:97%
复仇者联盟4下载进度:97%
复仇者联盟3下载进度:98%
复仇者联盟2下载进度:98%
复仇者联盟4下载进度:98%
复仇者联盟下载进度:98%
复仇者联盟3下载进度:99%
复仇者联盟2下载进度:99%
复仇者联盟4下载进度:99%
复仇者联盟下载进度:99%
复仇者联盟4下载进度:100%
复仇者联盟2下载进度:100%
复仇者联盟3下载进度:100%
复仇者联盟下载进度:100%
*/
}
package 第二种;
//实现是为了
public class Thunder implements Runnable {
//实现接口
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<101;i++) {
System.out.println(Thread.currentThread().getName()+"下载进度:"+i+"%");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
测试类
package 第二种;
public class RunableTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thunder t=new Thunder();//只是由线程的能力
//使用线程的能力包装下
Thread t1=new Thread(t,"钢铁侠");//传参
Thread t2=new Thread(t,"蜘蛛侠");//传参
Thread t3=new Thread(t,"美国堆中");//传参
Thread t4=new Thread(t,"海绵宝宝");//传参
Thread t5=new Thread(t,"神奇女侠");//传参
Thread t6=new Thread(t,"海王");//传参
//只有这个类才由start方法 启动子线程的功能
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
}
}
package 第二种;
public class Thunder1 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<101;i++) {
System.out.println(Thread.currentThread().getName()+i+"%");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
测试类
package 第二种;
public class Thunder1Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thunder1 a=new Thunder1();
Thread s1=new Thread(a,"钢铁侠");
s1.start();
Thread s2=new Thread(a,"绿巨人");
s2.start();
Thread s3=new Thread(a,"沐浴之王");
s3.start();
}
}
package 第二种1;
public class Thunder implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<101;i++) {
System.out.println(Thread.currentThread().getName()+i+"%");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package 第二种1;
public class ThunDerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thunder a=new Thunder();
Thread s1=new Thread(a,"哥谭市");
Thread s2=new Thread(a,"经历");
Thread s3=new Thread(a,"金刚狼");
Thread s4=new Thread(a,"我是你爸");
s1.start();
s2.start();
s3.start();
s4.start();
}
}