- 实现Runnable接口
-
实现Runnable
- 定义 MyRunnable类 实现 Runnable接口
- 重写run()方法,编写线性执行体
- 创建线程对象,调用 start() 方法启动线程
- 推荐使用Runnable对象,因为Java单继承的局限性
//创建线程方式2: 实现runnable接口,重写run()方法,执行线程需要丢入runnable接口实现类,调用start方法
public class ThreadTest03 implements Runnable{
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 10; i++) {
System.out.println("看书++++++++");
}
}
public static void main(String[] args) {
//创建runnable接口的实现类对象
ThreadTest03 threadTest03 = new ThreadTest03();
//创建线程对象,通过线程对象来开启我们的线程,代理
/*Thread thread = new Thread(threadTest03);
thread.start();*/
new Thread(threadTest03).start();
for (int i = 0; i < 200; i++) {
System.out.println("写字=============");
}
}
}
//一个对象
StartThread4 station = new StratThread4();
//多个线程
new Thread(station,"1").start();
new Thread(station,"2").start();
new Thread(station,"3").start();