线程的生命周期分为
1.新建(new Thread) 2. 就绪(runnable) 3. 运行(running) 4. 堵塞(blocked) 5. 死亡(dead)
创建线程的两个方法 第一种
package thread;
public class ThreadDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
}
}
class MyThread extends Thread{
public void run(){
System.out.println("线程启动了");
}
}
运行结果如下所示
第二种
package thread;
public class ThreadDemo2 {
public static void main(String[] args) {
MyRunnable r1 = new MyRunnable();
Thread t1 = new Thread(r1);
t1.start();
}
}
class MyRunnable implements Runnable{
public void run(){
System.out.println("线程启动了");
}
}
运行结果如下所示
|