- - , .
:
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.
swapy source
share