Can a java programmer can create daemon threads manually? How is it?
Asked
Active
Viewed 1.4k times
6
-
2Do you mean "daemon" threads ? In which case, google can take you further.. – Gishu Aug 14 '09 at 09:18
-
1Please note that this might not do what you're really after. A daemon thread isn't the same as a daemon process. – Emil H Aug 14 '09 at 09:45
-
Biju and http://stackoverflow.com/users/124339/johanna are same person? – sourcerebels Aug 14 '09 at 10:11
-
No i am a different person... – Biju CD Aug 14 '09 at 10:14
-
Stop flooding stackoverflow, go to your favourite search engine and use it, please. – sourcerebels Aug 14 '09 at 10:18
-
Wow why the hate? Perfectly reasonable question assuming they wanted to create a daemon thread... – rogerdpack Jun 11 '13 at 21:58
4 Answers
12
java.lang.Thread.setDaemon(boolean)
Note that if not set explicitly, this property is "inherited" from the Thread that creates a new Thread.

Gray
- 115,027
- 24
- 293
- 354

Michael Borgwardt
- 342,105
- 78
- 482
- 720
6
You can mark a thread as a daemon using the setDaemon method provided. According to the java doc:
Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.
This method must be called before the thread is started.
This method first calls the checkAccess method of this thread with no arguments. This may result in throwing a SecurityException (in the current thread).
Here an example:
Thread someThread = new Thread(new Runnable() {
@Override
public void run() {
runSomething();
}
});
someThread.setDaemon(true);
someThread.start();
0
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());
}
}
-4
Yes you can
Thread thread = new Thread(
new Runnable(){
public void run(){
while (true)
wait_for_action();
}
}
);
thread.start();

Lliane
- 831
- 2
- 7
- 15
-
1class Devil extends Thread { Devil() { setDaemon( true ); start(); } public void run() { // Perform evil tasks ... } } I got this one... Have u heard about this.. – Biju CD Aug 14 '09 at 09:26