java主線程結束和子線程結束之間的關系


 

(一)Main線程是個非守護線程,不能設置成守護線程。

              這是因為,main線程是由java虛擬機在啟動的時候創建的。main方法開始執行的時候,主線程已經創建好並在運行了。對於運行中的線程,調用Thread.setDaemon()會拋出異常Exception in thread "main"     java.lang.IllegalThreadStateException。測試代碼如下:

[java]  view plain  copy
 
  1. public class MainTest  
  2. {  
  3.     public static void main(String[] args)  
  4.     {  
  5.         System.out.println(" parent thread begin ");  
  6.         Thread.currentThread().setDaemon(true);  
  7.     }  
  8. }  

(二)Main線程結束,其他線程一樣可以正常運行。 

           主線程,只是個普通的非守護線程,用來啟動應用程序,不能設置成守護線程;除此之外,它跟其他非守護線程沒有什么不同。主線程執行結束,其他線程一樣可以正常執行。代碼如下:

[java]  view plain  copy
 
  1. public class ParentTest  
  2. {  
  3.   
  4.     public static void main(String[] args)  
  5.     {  
  6.         System.out.println("parent thread begin ");  
  7.           
  8.         ChildThread t1 = new ChildThread("thread1");  
  9.         ChildThread t2 = new ChildThread("thread2");  
  10.         t1.start();  
  11.         t2.start();  
  12.   
  13.         System.out.println("parent thread over ");  
  14.     }  
  15. }  
  16.   
  17. class ChildThread extends Thread  
  18. {  
  19.     private String name = null;  
  20.   
  21.     public ChildThread(String name)  
  22.     {  
  23.         this.name = name;  
  24.     }  
  25.   
  26.     @Override  
  27.     public void run()  
  28.     {  
  29.         System.out.println(this.name + "--child thead begin");  
  30.   
  31.         try  
  32.         {  
  33.             Thread.sleep(500);  
  34.         }  
  35.         catch (InterruptedException e)  
  36.         {  
  37.             System.out.println(e);  
  38.         }  
  39.   
  40.         System.out.println(this.name + "--child thead over");  
  41.     }  
  42. }  
  43.   
  44.   
  45. --程序運行結果如下:  
[java]  view plain  copy
 
  1. parent thread begin   
  2. parent thread over   
  3. thread2--child thead begin  
  4. thread1--child thead begin  
  5. thread2--child thead over  
  6. thread1--child thead over  

這樣其實是很合理的,按照操作系統的理論,進程是資源分配的基本單位,線程是CPU調度的基本單位。對於CPU來說,其實並不存在java的主線程和子線程之分,都只是個普通的線程。進程的資源是線程共享的,只要進程還在,線程就可以正常執行,換句話說線程是強依賴於進程的。也就是說,線程其實並不存在互相依賴的關系,一個線程的死亡從理論上來說,不會對其他線程有什么影響。

 

(三)Main線程結束,其他線程也可以立刻結束,當且僅當這些子線程都是守護線程。

       java虛擬機(相當於進程)退出的時機是:虛擬機中所有存活的線程都是守護線程。只要還有存活的非守護線程虛擬機就不會退出,而是等待非守護線程執行完畢;反之,如果虛擬機中的線程都是守護線程,那么不管這些線程的死活java虛擬機都會退出。測試代碼如下:

 

[java]  view plain  copy
 
  1. public class ParentTest  
  2. {  
  3.   
  4.     public static void main(String[] args)  
  5.     {  
  6.         System.out.println("parent thread begin ");  
  7.           
  8.         ChildThread t1 = new ChildThread("thread1");  
  9.         ChildThread t2 = new ChildThread("thread2");  
  10.         t1.setDaemon(true);  
  11.         t2.setDaemon(true);  
  12.           
  13.         t1.start();  
  14.         t2.start();  
  15.   
  16.         System.out.println("parent thread over ");  
  17.     }  
  18. }  
  19. class ChildThread extends Thread  
  20. {  
  21.     private String name = null;  
  22.     public ChildThread(String name)  
  23.     {  
  24.         this.name = name;  
  25.     }  
  26.     @Override  
  27.     public void run()  
  28.     {  
  29.         System.out.println(this.name + "--child thead begin");  
  30.         try  
  31.         {  
  32.             Thread.sleep(500);  
  33.         }  
  34.         catch (InterruptedException e)  
  35.         {  
  36.             System.out.println(e);  
  37.         }  
  38.         System.out.println(this.name + "--child thead over");  
  39.     }  
  40. }  
  41.    
  42.  執行結果如下:  
  43. parent thread begin  
  44. parent thread over  
  45. thread1--child thead begin  
  46. thread2--child thead begin  


在這種情況下,的確主線程退出,子線程就立刻結束了,但是這是屬於JVM的底層實現機制,並不是說主線程和子線程之間存在依賴關系


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM