菜雞的Java筆記 - java 執行緒常用操作方法
執行緒常用操作方法
執行緒的命名操作,執行緒的休眠,執行緒的優先順序
執行緒的所有操作方法幾乎都在 Thread 類中定義好了
執行緒的命名和取得
從本質上來講多執行緒的執行狀態並不是固定的。所以來講愛那個要想確定執行緒的執行,唯一的區別就在於執行緒的名稱上
在起名的時候就應該儘可能的避免重名,或者避免修改名稱
在 Thread 類中提供有如下的方法可以實現執行緒名稱的操作:
構造方法: public Thread(Runnable target, String name)
設定名字: public final void setName(String name)
取得名字: public final String getName()
既然執行緒的執行本身是不確定的狀態,所以如果要取得執行緒名字的話,那麼唯一能做的就是取得當前的執行緒名字
所以在 Thread 類裡面提供有這樣的方法: public static Thread currentThread()
範例:執行緒的命名和取得
package cn.mysterious.study3; class MyThread implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ",i = " + i); } } } publicclass StudyThread { public static void main(String[] args) throws Exception { MyThread mt = new MyThread(); new Thread(mt,"執行緒A").start(); new Thread(mt).start(); new Thread(mt).start(); } }
如果在設定執行緒物件是沒有設定具體的名字,那麼就採用一個預設的名字進行定義
範例:觀察程式碼
package cn.mysterious.study3; class MyThread implements Runnable{ @Override public void run() { System.out.println("MyThread 執行緒類:" + Thread.currentThread().getName()); } } public class StudyThread { public static void main(String[] args) throws Exception { MyThread mt = new MyThread(); new Thread(mt).start(); // 執行緒啟動呼叫 run() 方法 mt.run(); // 直接通過物件呼叫 run() 方法 } } /* 結果: MyThread 執行緒類:main MyThread 執行緒類:Thread-0 */
MyThread 執行緒類:main ( mt.run(); )
MyThread 執行緒類:Thread-0 (new Thread(mt).start();)
執行緒一定是依附在程序存在的,但是現在的程序在哪裡呢?
每當使用java命令在JVM上解釋某一個程式執行的時候,那麼都會預設的啟動一個JVM的程序,而主方法只是這程序中的一個執行緒,所以整個程式一直都跑線上程的執行機制上
每個JVM至少會啟動兩個執行緒:主執行緒,GC執行緒
執行緒的休眠
如果要想讓某些執行緒延緩執行,俺麼就可以使用休眠的方式來進行處理
在 Thread 類裡面提供有如下的休眠操作:
休眠方法: public static void sleep(long millis,int nanos)throws InterruptedException
如果休眠的時間沒到就停止休眠了,那麼就會產生中斷異常
範例:觀察休眠
package cn.mysterious.study3; class MyThread implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 10; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ",i = " + i); } } } public class StudyThread { public static void main(String[] args) throws Exception { MyThread mt = new MyThread(); new Thread(mt,"執行緒A").start(); new Thread(mt,"執行緒B").start(); new Thread(mt,"執行緒C").start(); } }
以上的程式碼執行中感覺像是所有的執行緒物件都同時休眠了。但是嚴格來講不是同時,是有先後順序的,只不過這個順序小一點而已
package cn.mysterious.study3; class MyThread implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 100; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ",i = " + i); } } } public class StudyThread { public static void main(String[] args) throws Exception { MyThread mt = new MyThread(); Thread t = new Thread(mt,"執行緒A"); t.start(); Thread.sleep(2000); t.interrupt(); // 中斷 } }
後續會使用休眠來進行執行緒的分析
執行緒的優先順序
從理論上來講優先順序越高的執行緒越有可能先執行。而在 Thread 類裡面定義有一下的優先順序操作方法:
設定優先順序: public final void setPriority(int newPriority)
取得優先順序: public final int getPriority()
而對於優先順序一共定義有三種:
最高優先順序: public static final int MAX_PRIORITY: 10
中等優先順序: public static final int NORM_PRIORITY: 5
最小優先順序: public static final int MIN_PRIORITY: 1
範例:觀察優先順序
package cn.mysterious.study3; class MyThread implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 100; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ",i = " + i); } } } public class StudyThread { public static void main(String[] args) throws Exception { MyThread mt = new MyThread(); Thread t1 = new Thread(mt,"執行緒A"); Thread t2 = new Thread(mt,"執行緒B"); Thread t3 = new Thread(mt,"執行緒C"); // 設定優先順序 t1.setPriority(Thread.MAX_PRIORITY); t2.setPriority(Thread.MIN_PRIORITY); t3.setPriority(Thread.MIN_PRIORITY); t1.start(); t2.start(); t3.start(); } }
範例:主執行緒的優先順序是什麼呢?
public class StudyThread { public static void main(String[] args) throws Exception { System.out.println(Thread.currentThread().getPriority()); } }
可以發現主執行緒屬於中等優先順序或者叫一般優先順序
總計
1.執行緒要有名字, Thread.currentThread 取得當前執行緒
2.執行緒的休眠是有先後順序的
3.理論上執行緒的優先順序越高越有可能先執行