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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:39:12+00:00 2026-05-23T09:39:12+00:00

When I strongly reference ThreadLocal holding a resource as shown in below example; private

  • 0

When I strongly reference ThreadLocal holding a resource as shown in below example;

private final static ThreadLocal<InputStream> resource = new ThreadLocal<InputStream>()
{
@Override
protected InputStream initialValue()
{
  ClassLoader loader = Thread.currentThread().getContextClassLoader();
  InputStream input = loader.getResourceAsStream("doc/doc.xml");
  if (input == null) throw new RuntimeException("Could not locate doc.xml");
  return input;
 }
};

At what point in time/scope will the InputStream held in the ThreadLocal not be avaliable. I want to know if at some point when the ThreadLocal object is not used, the JVM will garbage collect its reference and hence access to nested InputStream will not be available for usage.

The javadoc indicate that;

All of its copies of thread-local
instances are subject to garbage
collection (unless other references to
these copies exist).

If this is the case, then how do one make sure a copy is constantly available?

  • 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-23T09:39:13+00:00Added an answer on May 23, 2026 at 9:39 am

    The problem with your code is that you will keep a file pointer open. You can store Objects in a ThreadLocal, but do not store resources such as file handles, db connections or other resources that need to be closed. The ThreadLocal will go out of scope and therefore be garbage collected once the Thread has completed. In your case I would store the contents of what you are getting from the InputStream in the ThreadLocal but not the stream itself.
    If you want to make sure something is always available use the Singleton pattern. Be aware of the limitations of the pattern in the Java EE environment.

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

Sidebar

Related Questions

This question is strongly connected to this answer to How to reference a generic
For some reason, when I create a new view and select 'Create Strongly Typed
For example, using resharper in c# code I can perform a 'Move to resource'
Apple strongly recommends using the binary plist format when reading large XML-based data sets
I believe strongly in using unit-tests as part of building large multi-platform applications. We
I have a strongly-typed MVC View Control which is responsible for the UI where
I created a strongly typed LocationViewData with a properties like public Location location {
I have a strongly typed user control (partial) and I'd like to be able
There's no strongly typed View() method to return an ActionResult. So, suppose I have
I want a strongly-typed DataSet along with designer TableAdapters, but Visual Studio's DataSet designer

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.