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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:16:59+00:00 2026-06-14T05:16:59+00:00

I have the following structure: public void someMethod(){ //DO SOME STUFF try{ doSomeProcessing(); }

  • 0

I have the following structure:

public void someMethod(){  
   //DO SOME STUFF
   try{  
    doSomeProcessing();  
   }  
   catch (Exception e){  
        loadSomeHeavyData();  
        doSomeProcessing();      
   }    
}  

The method someMethod may be called concurrently by many threads. The doSomeProcessing may throw an exception (it is using some data in the backend that could become obsolete).
If an exception is thrown then loadSomeHeavyData(); does some timeconsuming task that let’s say “updates” all the current data and I am able to call doSomeProcessing();.
Problem: How can I make sure that loadSomeHeavyData(); is called only once? If I put some atomic flag in the entry of loadSomeHeavyData(); then I can not be sure when this should be cleared.
How can I solve this? Just a note: I can not modify doSomeProcessing(); as it is an external API and I am using decorator pattern to use it.

  • 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-14T05:17:01+00:00Added an answer on June 14, 2026 at 5:17 am

    Your loadSomeHeavyData method could use a blocking mechanism to make all threads wait until it has finished its update, but only let one of them actually do the update:

    private final AtomicBoolean updateStarted = new AtomicBoolean();
    private final CountDownLatch updateFinished = new CountDownLatch(1);
    
    public void loadSomeHeavyData() {
        if (updateStarted.compareAndSet(false, true)) {
            //do the loading
            updateFinished.countDown();
        } else {
            //update already running, wait
            updateFinished.await();
        }
    }
    

    Note my assumptions:

    • you want all the threads to wait until the loading completes so they can call doSomeProcessing a second time with updated data
    • you only call loadSomeHeavyData once, ever – if not you will need to reset the flag and the CountdownLatch (which would then probably not be the most appropriate mechanism).

    EDIT

    Your latest comment indicates that you actually want to call loadSomeHeavyData more than once, just not more than once at a time.

    private final Semaphore updatePermit = new Semaphore(1);
    
    public void loadSomeHeavyData() {
        if (updatePermit.tryAcquire()) {
            //do the loading and release updatePermit when done
            updatePermit.release();
        } else {
            //update already running, wait
            updatePermit.acquire();
            //release the permit immediately
            updatePermit.release();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following Interface structure; public interface IPaymentTypeBase { void PayNow(); double
I have a simple multi-threaded program, with the following structure. public void someFunction() {
I have the following code (OLE = OptimisticLockException) ... public void outer() { try
In Structure map I have the following line working with domain events: public void
I have the following structure: class A{ public: virtual void fn() = 0; }
I have the following structure: Model public class EventEntry : LogEntry { public EventType
I have the following class structure public class Outer{ private Mapper a; .... private
I'm using ASP.NET MVC2 and I have the following object structure: public class IDealer
I have following data structure (simplified): A giant list of the class TestClass public
I have the following function that prunes a tree data structure : public static

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.