02-创建线程的三种方式
继承Thread类
// 继承Thread
class MyThread extends Thread {
// 重写run方法执行任务
@Override
public void run() {
for (int i = 0; i < 10; i++) {
// 可以通过this拿到当前线程
System.out.println(this.getName()+"执行了"+i);
}
}
}
public class Demo_02_02_1_ThreadCreateWays {
public static void main(String[] args) {
// 先new出来,然后启动
MyThread myThread = new MyThread();
myThread.start();
for (int i = 0; i < 10; i++) {
// 通过Thread的静态方法拿到当前线程
System.out.println(Thread.currentThread().getName()+"执行了"+i);
}
}
}实现Runnable
使用Callable和Future
三种方式比较
最后更新于