package myPro1;
//实现线程的第二个办法,实现Runnable接口,复写run方法
//实际开发中,用这种比较多,因为继承能不用,就尽量不要用,
//因为Java是单继承,继承了这个,就不能继续别的类
public class RunnableImpl implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Runnable=> " + i);
}
}
}
package myPro1;
public class T1 {
public static void main(String[] args) {
RunnableImpl runnableImpl=new RunnableImpl();//Runnable接口的实现类对象
//把Runnable接口的实现类对象,传进Thread的构造方法
Thread thread=new Thread(runnableImpl);
thread.start();//启动线程
}
}
实现线程第二种方法(推荐)
本文介绍通过实现Runnable接口来创建线程的方法,并提供了一个具体的实现案例。这种方式避免了单继承带来的限制,使得代码更加灵活。

被折叠的 条评论
为什么被折叠?



