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 3615386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:22:01+00:00 2026-05-18T22:22:01+00:00

I have Ant build and execute a java program. This program tries to do

  • 0

I have Ant build and execute a java program. This program tries to do something that sometimes hangs, so we execute it in a thread.

actionThread.start();
try {
    actionThread.join(10000);
} catch (InterruptedException e) {
    System.out.println("InterruptedException: "+e.getMessage());
} 
if (actionThread.isAlive()) {
    actionThread.interrupt();
    System.out.println("Thread timed out and never died");
}

The ant call looks like this:

<java fork="true" failonerror="yes" classname="myPackage.myPathName" classpath="build">  
    <arg line=""/>
    <classpath>
        <pathelement location="bin" />
        <fileset dir="lib">
            <include name="**/*.jar"/>
        </fileset>
    </classpath>
</java>

And when this runs I see the “Thread timed out and never died” statement, and I also see the main program finish execution, but then Ant just hangs. Presumably it is waiting for the child threads to finish, but they never will.

How can I have Ant be done once it is done executing main() and just kill or ignore dead threads?

  • 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-18T22:22:01+00:00Added an answer on May 18, 2026 at 10:22 pm

    You can use public final void setDaemon(boolean on) method of the Thread class i.e actionThread.setDaemon(true). That way you will ensure that JVM exits once the main Thread is finished.

    Java Doc says:

    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.

    UPDATE


    System.exit() Vs Daemon thread

    For a bigger piece of code you cannot always be sure that all the active threads are the daemon threads. If a new thread is created by the daemon thread and if its setDaemon(true) is not set then it will inherit it from the parent. Although there can be scenarios where the newly created thread is set as a non-daemon (and then we will face your current problem).

    I personally think if you are done then you can call System.exit(0);

    As per Java doc for System.exit():

    Terminates the currently running Java
    virtual machine by initiating its
    shutdown sequence. This method never
    returns normally…In the first phase
    all registered shutdown hooks, if any,
    are started in some unspecified order
    and allowed to run concurrently until
    they finish. In the second phase all
    uninvoked finalizers are run if
    finalization-on-exit has been enabled.
    Once this is done the virtual machine
    halts.

    There are other SO post where they have discussed this:

    When should we call System.exit in Java

    System.exit() can be used to run
    shutdown hooks before the program
    quits. This is a convenient way to
    handle shutdown in bigger programs,
    where all parts of the program can’t
    (and shouldn’t) be aware of each
    other.

    From which thread should System.exit() be called in a Swing-app?

    The GC would take a while before
    things are rounded off and the app
    exits. There is nothing wrong with
    calling System.exit, once you dealt
    with closing what you ought to.

    As a third solution (1st is System.exit() , 2nd setDaemon()) you can also check for interrupted threads before doing any processing or decide ‘what to do’ in the handling of InterruptedException.

    Hope this will help.

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

Sidebar

Related Questions

I have some automatically generated Ant build scripts that I need to use to
I am trying to build sipdroid under eclipse,I have downloaded the source from the
Here's the problem: I have a java dynamic web project under source control and
I am currently working on a project that involves many subprojects that all have
I have just translated an ant project into maven however since maven does not
I know of ant coverage However that does a few things, it compiles with
I have installed Cassandra on my Ubuntu VM and built the source. I was
I have a run.haml file where I run a test suit. Everything works just
I'm just starting with Struts 1.3. I tested the struts-mailreader-1.3.10.war in Tomcat and everything

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.