Can we make the user thread as daemon thread if the thread is started?
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.
class Testdaemon1 extends Thread{
publicvoid run(){
System.out.println("Running thread is daemon...");
}
publicstaticvoid main (String[] args) {
Testdaemon1 td= new Testdaemon1();
td.start();
setDaemon(true);// It will throw the exception: td.
}
}
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)
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