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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:02:37+00:00 2026-05-24T07:02:37+00:00

I got a ScheduledExecutorService for task scheduling in a JEE environment. Some of those

  • 0

I got a ScheduledExecutorService for task scheduling in a JEE environment. Some of those task are leaving resources opened when they are interrupted with ScheduledExecutorService.shutdownNow() (e.g. open files with a third-party lib like Lucene).

I know that a thread may not stop his execution by itself: The must used way to stop a thread is cheeking the interrupt flag and stopping the method execution, and if the thread is block (e.g wait(), sleep(), etc) or if doing some IO operation in a interruptible channel the Thread.interrupt() will make a InterruptedException rise. In both cases, the finally block must be executed.
See: http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Thread.html#interrupt%28%29.

Obviously, I already tried to release the resources with a very well implemented finally block in the Task class, but in some environments (e.g. CentOS) the finally block is not executed when the thread is interrupted. And then I found this very cool note in the official Java Documentation:

Note: If the JVM exits while the try or catch code is being executed,
then the finally block may not execute. Likewise, if the thread
executing the try or catch code is interrupted or killed, the finally
block may not execute even though the application as a whole
continues.

So, what I need is a reference to all the scheduled task in order to implement some public method in the Task classes that force the release of resources. Can I retrieve those references to the task classes from the ScheduledExecutorService? Or do you have some cool idea to resolve my problem in a better way?

The first solution: Wrap it!

Create a Wrapper class for the ScheduledExecutorService and add a property like this:

private IdentityHashMap<ScheduledFuture<?>, Runnable> taskList;

With that we can access any Runnable object directly, or by the ScheduledFuture related to it. For the instantiation of the wrapper, I can get the ScheduledExecutorService from the Executors.newScheduledThreadPool() method and pass it to my wrapper.

Another Solution: Extend it!

Extend the ScheduledThreadPoolExecutor, add the IdentityHashMap property and overwrite all the method that schedules or cancels jobs to add/remove the reference from the Map.

The problem with both solutions?

If the caller of your wrapper or extended class receive a SchedulerFuture<?> object, cancel the job with the SchedulerFuture<?>.cancel() method is possible, bypassing your “capsule”. With the wrapper you can avoid passing the SchedulerFuture<?> reference to the caller, but with the extended class you can’t (if you create your own methods in the extended class you will get the same result as the wrapper, but in a very confusing way).

The elegant solution: Your own scheduler! Thanks to Kaj for pointing it …

  1. Extend the ScheduledThreadPoolExecutor to overwrite the
    decorateTask() method
  2. Decorate the Runnable with one implementation of a
    ScheduledFuture interface
  3. Implement one custom cancel() method that actually
    cancels the thread but also manipulates the Runnable object to force
    the resource releasing.

Check my blog post for the details and code exemples!!!

  • 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-24T07:02:38+00:00Added an answer on May 24, 2026 at 7:02 am

    What are you scheduling? What does the tasks look like? I find it very hard to believe that the finally block isn’t executed. I would guess that it’s the tasks that you have scheduled, but that haven’t started executing that are leaking resources (since their finally block won’t be executed)

    Sounds like a really bad VM implementation on the CentOS if it really aren’t executing those finally blocks. Haven’t heard about that in any other VM implementation.

    One option that you can do, instead of referencing all of the scheduled tasks, is to subclass ScheduledThreadPoolExecutor and override the decorateTask methods so that they decorate the tasks with your classes, and then intercept the cancel invokation.

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

Sidebar

Related Questions

Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
Got a task to make a program that registers animals and the object is
Got some problem with settings up the Authorization. First i got : <authorization> <deny
Got an application wich read some infos in a specific XML file. I would
Got some code here that isn't working: $(#sidebar ul li:last).each(function(){ $(this).addClass(last); }); Basically I
Got some great help yesterday with some php code for a mailer and forgot
Got solution - See bottom of the post Just want to know if there
Got a bit of a confusing situation, I have a form on my webpage
Got a bluescreen in windows while cloning a mercurial repository. After reboot, I now
got a new blog at wordpress few days ago ( http://ghads.wordpress.com ) and I

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.