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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:21:36+00:00 2026-06-07T21:21:36+00:00

I have a Static helper class implemented that helps cache and retreive some read-only,

  • 0

I have a Static helper class implemented that helps cache and retreive some read-only, non-mutable, non-volatile data from the database.

(Stripped) Example:

public class CacheHelper
{
    private static HashMap foos, bars;

    public static Foo getFoo(int fooId) { /* etc etc */ }
    public static Bar getBar(int barId) { /* etc etc */ }

    public static void reloadAllCaches()
    {
        //This is where I need it to lock access to all the other static methods
    }
}

The way I’ve read it for static classes, If I add the synchronized keyword to the reloadAllCaches() method, this will apply a lock on the entire class while that method executes. Is this correct? (Edit: Yep, not correct. Thanks for the responses. )

Note: I would like to remain agnostic to the thread safety of the getter methods and the objects they return as this data is never mutated and would like it to be returned as fast as possible.

  • 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-07T21:21:37+00:00Added an answer on June 7, 2026 at 9:21 pm

    If you add the synchronized keyword to the reloadAllCaches() function all other static functions in the class that got the synchronized keyword can’t execute while the reloadAllCaches() function is running.

    How ever non-static functions can execute, not matter if they got the synchronized keyword or not. Also all other functions without the synchronized keyword can execute.

    After all a function with the synchronized can be looked at like:

    public class Bar
    {
        public static void foo()
        {
            synchronized (Bar.class)
            {
                // your code
            }
        }
    }
    

    A non-static function with the synchronized keyword can be looked at like this:

    public class Bar
    {
        public void foo()
        {
            synchronized (this)
            {
                // your code
            }
        }
    }
    

    So static and non-static functions have a different synchronization context and do not block the execution of each other with the synchronized keyword.

    For your case I suggest the usage of a ReentrantReadWriteLock. This class will allow any number of functions to get a read-lock at the same time but only one function to get a Write-Lock. The write lock is only acquired when there is no read-lock in place and no read-lock is acquired as long as a write-lock is in place.

    You can make your reload function fetching a write-lock and all your reading function fetching a write-lock. You have to use a static instance of the ReentrantReadWriteLock of cause.

    My proposal is to implement it like this:

    public class CacheHelper
    {
        private static HashMap foos, bars;
        private static java.util.concurrent.locks.ReadWriteLock lock = new java.util.concurrent.locks.ReentrantReadWriteLock();
    
        public static Foo getFoo(int fooId)
        {
            lock.readLock().lock();
            try {
                /* etc etc */
            } finally {
                lock.readLock().unlock();
            }
        }
        public static Bar getBar(int barId)
        {
            lock.readLock().lock();
            try {
                /* etc etc */
            } finally {
                lock.readLock().unlock();
            }
        }
    
        public static void reloadAllCaches()
        {
            lock.writeLock().lock();
            try {
                //This is where I need it to lock access to all the other static methods
            } finally {
                lock.writeLock().unlock();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a helper class that I am using to obfuscate a static
I have the following helper class (simplified): public static class Cache { private static
I have a static class in my solution that is basically use a helper/ultility
Suppose I have a static helper class that I'm using a lot in a
I have a static class with a static constructor that takes some time (10-15
I have an object obj that is passed into a helper method. public static
I intended to create a class which only have static members and static functions.
I have a static class that I use as my Data Utils for my
In one STATIC class (my helper class named AutoItX3Delcarations.cs ) i have wrapped up
We have this class here: public static class Helper { private static readonly Random

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.