0

In python I can set a thread to be a daemon, meaning if parent dies, the child thread automatically dies along with it.

Is there an equivalent in Java?

Currently I am starting a thread like this in Java, but the underlying child thread does not die and hang even if main thread exits

         executor = Executors.newSingleThreadExecutor();
         executor.submit(() -> {
             while (true) {
                 //Stopwatch stopwatch = Stopwatch.createStarted();

                 String threadName = Thread.currentThread().getName();
                 System.out.println("Hello " + threadName);
                 try {
                     Thread.sleep(1*1000);
                 } catch (InterruptedException e) {
                     break;
                 }   

             }       
         });
3
  • 1
    Have you checked Thread.setDaemon? Commented Apr 3, 2019 at 8:26
  • In Java, the only difference between a daemon thread and a non-daemon thread is that the former won't keep the JVM alive. A daemon thread won't die because the thread that started it has died (unless, of course, the parent thread dying means there's no more non-daemon threads running). Commented Apr 3, 2019 at 8:31
  • In addition to @Slaw's comment; there is also no way to focibly kill a Thread in Java so such a thing could not be implemented. Commented Apr 3, 2019 at 8:35

1 Answer 1

1

When you're interacting with bare Thread you can use:

Thread thread = new Thread(new MyJob());
thread.setDaemon(true);

thread.start();

In your example, there's ExecutorService that needs to be provided with ThreadFactory which should do the similar job - like this:

Executors.newSingleThreadExecutor(new ThreadFactory() {
    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setDaemon(true);

        return thread;
    }
});

I would also recommend using Guavas ThreadFactoryBuilder:

Executors.newSingleThreadExecutor(
        new ThreadFactoryBuilder()
                .setDaemon(true)
                .build()
); 

It eases setting the most common thread properties and allows for chaining multiple thread factories

update

As Slaw and Boris the Spider rightfully noticed - you have mentioned the behavior that would cause killing child-thread when parent-thread dies. There's nothing like that either in Python or Java. Daemon threads will be killed when all other non-daemon threads exited.

Sign up to request clarification or add additional context in comments.

7 Comments

These are not the same thing. The OP says in Python that if the parent dies, the child is killed.
@BoristheSpider I don't think there's something like that in Python :) Thread's parent is the process spawning it
That is exactly my point. This answer is not only wrong it is misleading. Your comment about the thread's parents is of course incorrect.
it looks like my submit() isnt being called anymore after changing to using new ThreadFactory(). am i missing something?
@ealeon In a thread factory, you should pass Runnable to a Thread constructor (like that: Thread thread = new Thread(r);) I have updated the code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.