How to create daemon threads?

Can java programmer create daemon threads manually? Like this?

+6
java daemon
source share
4 answers

java.lang.Thread.setDaemon (boolean)

Note that unless specified explicitly, this property is "inherited" from Thread, which creates a new thread.

+13
source share

You can mark the stream as a daemon using the provided setDaemon method. According to java doc:

Marks this thread as a daemon thread or user thread. The Java virtual machine exits when all threads executed only by a thread are daemon threads.

This method must be called before the stream begins.

This method first calls the checkAccess method of this stream with no arguments. This can throw a SecurityException (in the current thread).

Here is an example:

Thread someThread = new Thread(new Runnable() { @Override public void run() { runSomething(); } }); someThread.setDaemon(true); someThread.start(); 
+6
source share
 class mythread1 implements Runnable { public void run() { System.out.println("hii i have set thread as daemon"); } public static void main(String []arg) { mythread1 th=new mythread1(); Thread t1 = new Thread(th); t1.setDaemon(true); t1.start(); System.out.println(t1.isDaemon()); } } 
0
source share

Yes, you can

 Thread thread = new Thread( new Runnable(){ public void run(){ while (true) wait_for_action(); } } ); thread.start(); 
-4
source share

All Articles