public class MyThread extends Thread {
public static void main(String[] args) {
Thread a = new MyThread(); //(1)
a.start(); //(2)
System.out.println("This is main thread."); //(3)
}
public void run(){
System.out.println("This is another thread."); //(4)
}
}
MyThread 类继承了 Thread 类,并覆盖了 run 方法。调用 start 方法后,当操作系统分配时间片给这个线程后,线程启动。注释点(3)和(4)代码行是并发执行的。