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

  • SEARCH
  • Home
  • 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 7843741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:38:50+00:00 2026-06-02T16:38:50+00:00

I have a static, periodic, java Timer/TimerTask that I would like to shutdown when

  • 0

I have a static, periodic, java Timer/TimerTask that I would like to shutdown when the app does. I don’t want the app hanging because some thread is still running (like what happens in debug mode in eclipse, some environments may kill the thing anyway). The reason I have it static is I plan to have some (very simple, probably just a counter) shared memory in all of the containing class’s instances with the Timer so I feel class scope is appropriate.

My question is how best to do the shutdown of the Timer? Is this an appropriate time to use finalize? This timer seems benign enough that having a non-deterministic call to finalize may work? Would probably need to do some kind of instance counting to verify that there are no longer any instances of the class out there? Suggestions on ways to manage the shutdown of the static Timer are welcome.

pseudo code:

class foo {

private static Timer someTimer = null;

public foo() {
  if(someTimer == null) {
     someTimer = new Timer(new TimerTask(...));
  }

}

//how should I shut this thing down?
protected void finalize() throws Throwable {

}

//or is better to have shutdown() called explicitly?

}
  • 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-06-02T16:38:52+00:00Added an answer on June 2, 2026 at 4:38 pm

    It all depends on what your app actually does, but in general there will be some kind of event to signal that the app is being shutdown. For example if it’s a GUI app, then maybe this will be the “user clicked on the Quit button” event. Or it’s a webapp based on the servlet API, it will be an event fired by a ServletContextListener.

    You should add a listener for this event, which calls some kind of shutdown method on your foo object. Inside this shutdown method the foo should take care of cleaning up its resources, including stopping the timer.

    As a last resort, you might want to investigate JVM shutdown hooks

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

Sidebar

Related Questions

I've read that JavaScript does not really have static variables(when i say static, I
1)When I have Static void Sample<T>(T a,T b) Does the declaration Sample enforce that
Is there a way to have a static class have static data that does
i've UITableViewController which have static cell. I would like to show another detailed view
In my CakePHP app I have static pages set up like this: Router::connect( '/terms',
Writing a PHP app and have several classes that only have static methods (no
I have several service classes that have static methods and offer a service to
Suppose I have static ip in a subnet that has DHCP server. If i
I want to have static and constant two dimensional array inside a class. The
I'm not referring to textInput, either. I mean that once you have static text

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.