Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Rewrite CalcMemoized using FutureTask #53

Open
skapral opened this issue Aug 31, 2020 · 0 comments
Open

Rewrite CalcMemoized using FutureTask #53

skapral opened this issue Aug 31, 2020 · 0 comments

Comments

@skapral
Copy link
Member

skapral commented Aug 31, 2020

Subj. This would simplify the solution's maintenance.

Prototype.

class Util {
    public static <T> T memoizedCalculation(ConcurrentHashMap<Key, FutureTask<T>> cache, Key key, Callable<T> calculation) {
        try {
            FutureTask<T> t = cache.computeIfAbsent(key, k -> new FutureTask<>(calculation));
            t.run();
            return t.get();
        } catch(Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant