Thread daemon?

What will happen to the thread, which is considered as Daemon?

What will be the effect of this flow?

What is "can and can'ts" in a stream?

+3
source share
5 answers

The main difference between a daemon thread and a non-daemon thread is that the program terminates when all non-daemon threads terminate. So, if you have an active daemon thread and end your first thread, the program terminates. Thus, you want to use the daemon thread for what you want to continue while the program is running.

+4
source

A - , JVM , , . Daemon , ,

, .

, , , setDaemon(true) Thread.


,

, setDaemon() start() invoked.Once (.. start() ) . , isDaemon().

+6

, Daemon?

isDaemon() true.

; .

?

, isDaemon().

"can and can'ts" ?

- , .
, .

+1

Daemon Non Daemon Java:

1) JVM .

2) Thread , User Thread, JVM , , , , JVM .

0

- - , .

: Java , " main", main. (-) . , , , -. (-) , "main" !

class HelloThread extends Thread  
{  
  public void run()  
  {  
    for ( ; ; )  
    {  
      System.out.println("hello");  
      sleep(1000);  
    }  
  }  
}  
public class RegularThreader  
{  
  public static void main(String[] args)  
  {  
    Thread hello = new HelloThread();  
    hello.start();  
    System.out.println("Sorry, I must be leaving");  
  }  
}  

, . "" , . . Daemon , ,

 public class DaemonThreader  
    {  
      public static void main(String[] args)  
      {  
        Thread hello = new HelloThread();  
        hello.setDaemon(true);  
        hello.start();  
        System.out.println("Sorry, I must be leaving");  
      }  
}  

Try running two different classes above and see how the output is different. A classic example of a daemon thread is the garbage collector found in many Java virtual machines. It should work continuously while all other threads are running, but should not prevent the program from exiting. When the program exits, there is no longer a need for a garbage collector.

0
source

All Articles