1,extends Thread;java
public class ThreadTest extends Thread{ //繼承方式run方法若是不寫,不報錯,運行t1.start方法爲空 @Override public void run() { // TODO Auto-generated method stub System.out.println(getName()); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ThreadTest t1 = new ThreadTest(); t1.start(); } }
2,implements runnable;多線程
public class RunnableTest implements Runnable { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub RunnableTest r1 = new RunnableTest(); Thread t1 = new Thread(r1); t1.start(); } //實現方式run方法若是不寫,報錯 @Override public void run() { // TODO Auto-generated method stub System.out.println(Thread.currentThread().getName()); } }
Thread.currentThread().setPriority(1-10);ide
設置優先級是沒有保障的,通過本地測試,優先級爲1的線程比優先級爲10的先執行完也是有可能的。測試
控制線程線程