【多線程】_線程常用操作方法筆記
本章目標:
瞭解設定和取得線程名稱
瞭解線程的強制運行
瞭解線程的休眠
瞭解線程的禮讓
瞭解線程的中斷操作
3.1取得和設定線程的名稱
在Thread類中,可以通過getName()方法取得線程的名稱,通過setName()方法設定線程的名稱。
線程的名稱一般線上程啟動前設定,但也允許啟動並執行線程設定名稱。允許兩個Thread對象有相同的名稱,但為了清晰,應該盡量避免這種情況發生。
另外,如果程式中沒有為線程指定名稱,則系統會自動的為線程分配一個名稱。
執行個體一:
class MyThread implement Runnable{ public void run(){ for(int i=0;i<3;i++){ System.out.println(Thread.currentThread().getName()+"運行,i = "+i); //取得當前線程的名字 } }}public class ThreadNameDemo{ public static void main(String[] args){ MyThread mt1 = new MyThread(); //執行個體化Runnable對象 new Thread(mt1).start(); new Thread(mt1,"線程-A").start(); new Thread(mt1,"線程-B").start(); new THread(mt1).start(); }}
3.2取得當前線程
class MyThread implement Runnable{ public void run(){ for(int i=0;i<3;i++){ System.out.println(Thread.currentThread().getName()+"運行,i = "+i); //取得當前線程的名字 } }}public class ThreadNameDemo{ public static void main(String[] args){ MyThread mt1 = new MyThread(); //執行個體化Runnable對象 new Thread(mt1,"線程-A").start(); mt1.run(); }}
輸出:
線程運行,i=0
線程運行,i=1
線程運行,i=2
main運行,i=0
main運行,i=1
main運行,i=2
mt1.run();調用此語句是由主方法完成的,也就是說實際上主方法本身也是一個線程——主線程
問題:既然主方法都是以線程的形式出現的,那麼JAVA運行時到底啟動了多少個線程呢?
回答:至少兩個(主線程,GC)
3.3、判斷線程是否啟動
class MyThread implements Runnable{ public void run(){ for(int i=0;i<3;i++){ System.out.println(Thread.currentThread().getName()+"運行,i="+i); } }}public class ThreadAliveDemo{ public static void main(String[] args){ MyThread mt = new MyThread(); //執行個體化Runnable子類對象 Thread t = new Thread(mt,"線程"); //執行個體化Thread對象 System.out.println("線程開始執行之前 -->"+t.isAlive()); t.start(); //啟動線程 System.out.println("線程開始執行之後 -->"+t.isAlive()); for(int i=0;i<3;i++){ System.out.println("main運行-->"+i); } //以下的輸出結果不正確 System.out.println("代碼執行之後-->" + t.isAlive()); }}
3.4、線程的強制運行
線上程操作中,可以使用join()方法讓一個線程強制運行,線程強制運行期間,其他線程無法運行,必須等待此線程完成之後才可以繼續執行。
main線程與Thread線程的強制運行執行個體:
class MyThread implements Runnable{ public void run(){ for(int i=0;i<3;i++){ System.out.println(Thread.currentThread().getName()+"運行,i="+i); } }}public class ThreadJoinDemo{ public static void main(String[] args){ MyThread mt = new MyThread(); Thread t = new Thread(mt,"線程"); t.start(); //啟動線程 for(int i=0;i<50;i++){ if(i>10){ try{ t.join(); //線程強制運行 }catch(InterruptedException e){ e.printStackTrace(); } } System.out.println("Main線程運行-->"+i); } }}
3.5、線程的休眠
使用休眠可以讓線程暫停執行
使用Thread.sleep()方法即可!
class MyThread implements Runnable{ public void run(){ for(int i=0;i<50;i++){ try{ Thread.sleep(500); }catch(InterruptedException e){ System.out.println(Thread.currentThread().getName()); } } }}public class ThreadSleepDemo{ public static void main(String[] args){ MyThread mt = new MyThread(); //執行個體化Runnable子類對象 Thread t = new Thread(mt); t.start(); //啟動線程 }}
3.6線程的中斷
一個線程被另一個線程中斷其操作的狀態,使用interrupt()完成操作
class MyThread implements Runnable{ public void run(){ System.out.print("1、進入run()方法"); try{ Thread.sleep(10000); //線程休眠10秒 System.out.println("2、已經完成休眠"); }catch(InterruptedException e){ System.out.println("3、休眠被終止"); return ; //返回調用處 } System.out.println("4、run()方法正常結束"); }}public class ThreadInterruptedDemo{ public static void main(String[] args){ MyThread mt = new MyThread(); Thread t = new Thread(mt,"線程"); t.start(); try{ Thread.sleep(2000); //主線程休眠2秒 }catch(InterruptedException e){ System.out.println("3、休眠被終止"); } t.interrupt(); //中斷線程執行 }}
3.7、後台線程
在java中,只要一個程式沒有執行完(一個線程在運行),則整個java的進程不會消失,所以,此時可以設定一個後台線程,這樣即使java進程結束了,此後台線程依然會繼續執行。
要想實現這樣的操作,直接使用setDaemo()方法即可。
class MyThread implements Runnable{ public void run(){ while(true){ System.out.println(Thread.currentThread().getName()+"在運行。"); } }}public class ThreadDaemonDemo{ public static void main(String[] args){ MyThread mt = new MyThread(); Thread t = new Thread(mt,"線程"); t.setDaemon(true); //此線程在後台運行 t.start(); //啟動線程 }}
3.8、線程的優先順序
最高優先順序 MAX_PRIORITY
中等優先順序 NORM_PRIORITY
最低優先順序 MIN_PRIORITY
class MyThread implements Runnable{ public void run(){ for(int i=0;i<5;i++){ try{ Thread.sleep(500); }catch(InterruptedException e){ } System.out.println(Thread.currentThread().getName()+"運行,i="+i); } }}public class ThreadPriorityDemo{ public static void main(String[] args){ Thread t1 = new Thread(new MyThread(),"線程A"); Thread t2 = new Thread(new MyThread(),"線程B"); Thread t3 = new Thread(new MyThread(),"線程C"); t1.setPriority(Thread.MAX_PRIORITY); t2.setPriority(Thread.NORM_PRIORITY); t3.setPriority(Thread.MIN_PRIORITY); }}
主線程main()的優先順序為NORM_PRIORITY
3.9、線程的禮讓
線上程的操作中,可以使用yield()方法將一個線程的操作暫時讓給其他線程操作
class MyThread implements Runnable{ public void run(){ for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName()+"運行,i"); if(i==3){ System.out.println("線程禮讓:"); Thread.currentThread().yield(); //線程禮讓 } } }}public class ThreadYieldDemo{ public static void main(String args[]){ MyThread my = new MyThread(); Thread t1 = new Thread(my,"線程A"); Thread t2 = new Thread(my,"線程B"); t1.start(); t2.start(); }}