Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6995795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:04:31+00:00 2026-05-27T20:04:31+00:00

Hypothetical scenario: I have a daemon thread responsible for some I/O, the main thread

  • 0

Hypothetical scenario:
I have a daemon thread responsible for some I/O, the main thread finishes and returns, and the JVM decides to terminate my daemon thread.

How does it do so? Interrupt? Finalize? How can I code my daemon thread so that it reacts gracefully when terminated?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:04:31+00:00Added an answer on May 27, 2026 at 8:04 pm

    I just wrote the following code as a test:

    public class DaemonThreadPlay {
        public static void main(String [] args) {
            Thread daemonThread = new Thread() {
                public void run() {
                    while (true) {
                        try {
                            System.out.println("Try block executed");
                            Thread.sleep(1000l);
                        } catch (Throwable t) {
                            t.printStackTrace();
                        }
                    }
                }
    
                @Override
                public void finalize() {
                    System.out.println("Finalize method called");
                }
            };
            daemonThread.setDaemon(true);
            daemonThread.start();
    
            try {
                Thread.sleep(2500l);
            } catch (Throwable t) {
                //NO-OP
            }
        }
    }    
    

    I put breakpoints in the catch block of the daemon thread and in the finalize method. Neither breakpoint was reached even though the try block was executed. Obviously this code has synchronization/timing issues, but I think we can safely conclude that daemon threads are not interrupted on shutdown nor are their finalize() methods necessarily invoked.

    You can always add a shutdown hook to the JVM Runtime:

    Thread shutdownHook = ... // construct thread that somehow
                              // knows about all the daemon threads
    Runtime.getRuntime().addShutdownHook(shutdownHook);
    

    Your shutdown hook can obviously do whatever tasks are required for a “graceful” shutdown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hypothetical scenario: I have two models: Author and Book . The Book model has
I have the following (hypothetical) scenario: Application to report on students in classes in
Maybe some might find this, a hypothetical question. I have recently started development of
Consider a hypothetical case where I have to retrieve some details from the database
In the hypothetical scenario where I have a couple of database tables, in a
Hypothetical situation: let's say I have a 3rd party .net assembly being used in
Hypothetical situation. I have a command line program in *nix (linux, BSD, etc.). It
Consider a hypothetical method of an object that does stuff for you: public class
Does anybody know what hypothetical indexes are used for in sql server 2000? I
While fleshing out a hypothetical domain model, I have found myself wondering whether the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.