Can we make the user thread as daemon thread if the thread is started?

Answer

No, if you do so, it will throw IllegalThreadStateException. Therefore, we can only create a daemon thread before starting the thread.

 
  1. class Testdaemon1 extends Thread{    
  2. public void run(){  
  3.           System.out.println("Running thread is daemon...");  
  4. }  
  5. public static void main (String[] args) {  
  6.       Testdaemon1 td= new Testdaemon1();  
  7.       td.start();  
  8.       setDaemon(true);// It will throw the exception: td.   
  9.    }  
  10. }  

Output

Running thread is daemon...

Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Thread.java:1359)
at Testdaemon1.main(Testdaemon1.java:8)

All Java Questions

Ask your interview questions on java

Write Your comment or Questions if you want the answers on java from java Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---