1. 程式人生 > >深入理解start()和run()方法

深入理解start()和run()方法

package com.tml.javaCore.thread;

/**
 * <p>執行緒start()/run()方法比較
 * @author Administrator
 *
 */
public class StartAndRunDemo {
	public static void main(String[] args) {
		Thread t1 = new  MyThread("thread_01");
		System.out.println("run() ....");
		t1.run();
		System.out.println("start() ....");
		t1.start();
		
		//run()方法可以重複執行
		//t1.run();
		//start()方法不能重複執行,重複執行會丟擲java.lang.IllegalThreadStateException
		//t1.start();
		
	}
	
	static class  MyThread  extends Thread{
		public  MyThread(String name){
			super(name);
		}
		
		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName() + " is running!");
		}
	}

}