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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:07:35+00:00 2026-05-26T07:07:35+00:00

Here is the proposed solution (I did search for the same – unsuccessfully) public

  • 0

Here is the proposed solution (I did search for the same – unsuccessfully)

    public abstract class AsyncCache<T> {

        /**
         * an atomic int is used here only because stamped reference doesn't take a long,
         * if it did the current thread could be used for the stamp.
         */
        private AtomicInteger threadStamp = new AtomicInteger(1);
        private AtomicStampedReference<T> reference = new AtomicStampedReference<T>(null, 0);

        protected abstract T rebuild();

        public void reset() {
            reference.set(null, 0);
        }

        public T get() {
            T obj = reference.getReference();
            if (obj != null) return obj;

            int threadID = threadStamp.incrementAndGet();

            reference.compareAndSet(null, null, 0, threadID);

            obj = rebuild();

            reference.compareAndSet(null, obj, threadID, threadID);

            return obj;

        }

    }

The process should be easy to see – the resource is only built when requested and invalidated by calling reset.

The first thread to request resource inserts its ID into the stamped reference and will then insert its version of the resource once generated UNLESS another reset is called.
in case of a subsequent reset the first requesting thread will return a stale version of the resource (is valid use case), and some request started after the latest reset will populate the reference with its result.

Please let me know if I’ve missed something or if there is a better (faster + simlpler ++ elegant) solution.
One thing – the MAX_INT is not handled intentionally – don’t believe the prog will live long enough, but certainly easy to do.

Thank you.

  • 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-26T07:07:36+00:00Added an answer on May 26, 2026 at 7:07 am

    Thats defiently not async since requesting thread will block until rebuild() method is complete. Another problem – you don’t check value returned from compareAndSet. I believed you need something like this

    if(reference.compareAndSet(null, null, 0, threadID)) { //if resource was already reseted - rebuild
       reference.compareAndSet(null, obj, threadID, threadID);
       obj = rebuild();
    } 
    

    But that approach has another drawback – you have to rebuild entry several times (considering several threads wants that entry at once). You can use future tasks for that case (http://www.codercorp.com/blog/java/simple-concurrent-in-memory-cache-for-web-application-using-future.html) or use MapMaker.

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

Sidebar

Related Questions

I have 3 entities: @Entity public abstract class A { @Id public Long id;
Here's my proposed (very simplified to illustrate the problem space) design for a C#
I just found the MVC was initially proposed in 88 [ here is the
I tried to implement the solution proposed by T. Stone on my question how-do-i-pass-a-lot-of-parameters-to-views-in-django
Here are my schematic models: class Law(models.Model): ... class Bill(models.Model): ... # data for
I'm going through Casting SPELs in Lisp and this is the proposed solution to
Regarding the answer provided at this link: Proposed solution I tried to use this
I like the random color generation algorithm proposed here by David Crow , but
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form

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.