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的底层实现机制,并不是说主线程和子线程之间存在依赖关系

posted @ 2018-01-05 08:44  邱明成  阅读(21128)  评论(2编辑  收藏  举报