创建线程目前共四种方式:
继承Thread类 、实现Runable接口、实现Callable接口、由线程池创建
一、继承Thread类并重写run方法:
run()方法为线程具体执行内容
值得一提的是Thread实现了Runnable接口
public class ThreadDemo {
public static void main(String[] args) {
Thread thread01 = new MyThread();
Thread thread02 = new MyThread();
thread01.setName("小明");
thread02.setName("李华");
//启动线程 就绪状态
thread01.start();
thread02.start();
}
}
class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("线程名:"+Thread.currentThread().getName()+
"----线程id:"+Thread.currentThread().getId());
}
}
}