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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:33:48+00:00 2026-06-10T07:33:48+00:00

I’ve recently started playing with the LoadingCache class and I am getting some unexpected

  • 0

I’ve recently started playing with the LoadingCache class and I am getting some unexpected behaviour. I now know the solution, but I’m posting the question so others can learn from it.

Basically, I put a non-null value in the cache for a certain key. The key is the value’s position in a file and using the CacheLoader.load( ) method, the value can be read from file. At the moment the LoadingCache is used by a single thread.

When I run my application, the removal notification listener is called with a null value for a key that was previously used to insert a non-null value. I know this because I used assert statements to check that all values that go into the cache are not null (this shouldn’t happen anyway). Also I can see this because I printed the key in the method that gets called by CacheLoader.load( ): CacheLoader.load( ) is the only method that can call the method that prints the key. Furthermore, this method cannot return null values.

I’d be much obliged if somebody could let me know if I’m doing something wrong.

The following is my definition of the loading cache.

    private LoadingCache<Long,T> gcache( ) {
        @SuppressWarnings( Constants.UNCHECKED )
        final CacheLoader<Long,T> loader =
            new CacheLoader<Long,T>() {
                public T load(Long key) {
                    // This following method _did_ print the key for which
                    // the RemovalNotification listener returns a null value.
                    // See output further on.
                    return getElement( key );
                }
            };
        @SuppressWarnings( Constants.UNCHECKED )
        final RemovalListener<Long,T> listener
            = new RemovalListener<Long,T>( ) {
                  @Override
                  public void onRemoval( RemovalNotification<Long,T> notification ) {
                      final T value = notification.getValue( );
////////////////////////////////////////////////////////////////////////////
if (value == null) {
System.out.println( "????: " + notification.getKey( ) );
}
assert( value != null);
////////////////////////////////////////////////////////////////////////////
                      if (!value.immutable( )) {
                          put( notification.getKey( ), value );
                      }
                  }
              };
        @SuppressWarnings( Constants.UNCHECKED )
        final LoadingCache<Long, T> gcache
            = CacheBuilder.newBuilder( )
                          .softValues( )
                          .removalListener( listener )
                          .build( loader );
        return gcache;
    }

    private T getElement( long pos ) {
        // Critical section, but at the moment there's only one thread.
        enter( );
        seek( pos );
        final T creation = inflator.inflate( this );
///////////////////////////////////////////////////////////////////////
System.out.println( "getElement: pos = " + pos );
assert( creation != null );
///////////////////////////////////////////////////////////////////////
        leave( );
        return creation;
    }

Some of the output:

[ cut ]
getElement: pos = 362848
[ cut ]
????: 362848

After the question marks I get an uncontrollable amount of output, and the JVM is difficult to stop. I have to hit the key several times before I can terminate the application. (Of course kill would also do the trick, but the keyboard is slightly faster.)

  • 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-10T07:33:49+00:00Added an answer on June 10, 2026 at 7:33 am

    I’m just guessing: In the javadoc to RemovalNotification it reads

    A notification of the removal of a single entry. The key and/or value may be null if they were already garbage collected.

    and you’re using softValues(). When the value gets GC’ed, then the whole entry gets removed from the cache and the key may get collected as well.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.