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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:56:20+00:00 2026-05-15T17:56:20+00:00

I have a computing map (with soft values ) that I am using to

  • 0

I have a computing map (with soft values) that I am using to cache the results of an expensive computation.

Now I have a situation where I know that a particular key is likely to be looked up within the next few seconds. That key is also more expensive to compute than most.

I would like to compute the value in advance, in a minimum-priority thread, so that when the value is eventually requested it will already be cached, improving the response time.

What is a good way to do this such that:

  1. I have control over the thread (specifically its priority) in which the computation is performed.
  2. Duplicate work is avoided, i.e. the computation is only done once. If the computation task is already running then the calling thread waits for that task instead of computing the value again (FutureTask implements this. With Guava’s computing maps this is true if you only call get but not if you mix it with calls to put.)
  3. The “compute value in advance” method is asynchronous and idempotent. If a computation is already in progress it should return immediately without waiting for that computation to finish.
  4. Avoid priority inversion, e.g. if a high-priority thread requests the value while a medium-priority thread is doing something unrelated but the the computation task is queued on a low-priority thread, the high-priority thread must not be starved. Maybe this could be achieved by temporarily boosting the priority of the computing thread(s) and/or running the computation on the calling thread.

How could this be coordinated between all the threads involved?


Additional info
The computations in my application are image filtering operations, which means they are all CPU-bound. These operations include affine transforms (ranging from 50µs to 1ms) and convolutions (up to 10ms.) Of course the effectiveness of varying thread priorities depends on the ability of the OS to preempt the larger tasks.

  • 1 1 Answer
  • 2 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-15T17:56:20+00:00Added an answer on May 15, 2026 at 5:56 pm

    You can arrange for “once only” execution of the background computation by using a Future with the ComputedMap. The Future represents the task that computes the value. The future is created by the ComputedMap and at the same time, passed to an ExecutorService for background execution. The executor can be configured with your own ThreadFactory implementation that creates low priority threads, e.g.

    class LowPriorityThreadFactory implements ThreadFactory
    {
       public Thread newThread(Runnable r) {
         Tread t = new Thread(r);
         t.setPriority(MIN_PRIORITY);
         return t;
       }
    }
    

    When the value is needed, your high-priority thread then fetches the future from the map, and calls the get() method to retrieve the result, waiting for it to be computed if necessary. To avoid priority inversion you add some additional code to the task:

    class HandlePriorityInversionTask extends FutureTask<ResultType>
    {
       Integer priority;  // non null if set
       Integer originalPriority;
       Thread thread;
       public ResultType get() {
          if (!isDone()) 
             setPriority(Thread.currentThread().getPriority());
          return super.get();
       }
       public void run() {
          synchronized (this) {
             thread = Thread.currentThread();
             originalPriority = thread.getPriority();
             if (priority!=null) setPriority(priority);
          } 
          super.run();
       }
       protected synchronized void done() {
             if (originalPriority!=null) setPriority(originalPriority);
             thread = null;
       }
    
       void synchronized setPriority(int priority) {
           this.priority = Integer.valueOf(priority);
           if (thread!=null)
              thread.setPriority(priority);
       }
    }
    

    This takes care of raising the priority of the task to the priority of the thread calling get() if the task has not completed, and returns the priority to the original when the task completes, normally or otherwise. (To keep it brief, the code doesn’t check if the priority is indeed greater, but that’s easy to add.)

    When the high priority task calls get(), the future may not yet have begun executing. You might be tempted to avoid this by setting a large upper bound on the number of threads used by the executor service, but this may be a bad idea, since each thread could be running at high priority, consuming as much cpu as it can before the OS switches it out. The pool should probably be the same size as the number of hardware threads, e.g. size the pool to Runtime.availableProcessors(). If the task has not started executing, rather than wait for the executor to schedule it (which is a form of priority inversion, since your high priority thread is waiting for the low-priority threads to complete) then you may choose to cancel it from the current executor and re-submit on an executor running only high-priority threads.

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

Sidebar

Related Questions

Suppose I have a Java Servlet that takes a while to finish computing it's
I have a file that contains the following: #include <map> class A {}; void
I have an application that spends about 80% of its time computing the centroid
I have integer values ranging from 32-8191 which I want to map to a
I have an OSG application that I want to texture map a full screen
I have been reading up on Cloud computing on here and still not getting
The problem is this: I have multiple competing threads (100+) that need to access
Can someone tell me whether MSMQ (using transactions) supports competing consumers? Basically, I have
I have an MKMapView which allows the user to scroll the map around. Later,
I have the following: #include<iostream> #include<unordered_map> #include<tuple> using namespace std; class CTest { //

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.