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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:13:25+00:00 2026-05-12T11:13:25+00:00

I have a class which contains a static collection that can be used across

  • 0

I have a class which contains a static collection that can be used across all threads. It acts as a cache. When it is first loaded from the database, it is supposed to never change. However, in rare cases, we have to clear this cache. The current way to do this is to restart the server. I want to implement a clear cache method. What is the best way to do this?

Example class:

public class DepartmentRepository {
    private static Collection<Department> departments = new ArrayList<Department>();

    public synchronized Collection<Department> getAllDepartments() {
        if (departments.isEmpty()) {
            //do database lookup
        }
        return departments;
    }
    //more methods excluded
}

I want to add a method that looks like this:

public void clearCache() {
    departments.clear();
}

But I need to make sure it clears it across all threads and doesn’t break anything.

  • 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-12T11:13:25+00:00Added an answer on May 12, 2026 at 11:13 am

    In response to a follow-up comment by the OP:

    Is there performance hit with using a synchronizedList? This is at the core of a massively overgrown architecture.

    Yes, there is a performance hit. You’d have to measure it to find out how much. For one thing, synchronizing every operation to the Department collection might result in a lot of contention even when most of the operations are read-only. For thread safety with performance, consider something like this:

    import java.util.Collection;
    import java.util.Collections;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    
    public class DepartmentRepository
    {
    
      private static final ReentrantReadWriteLock lock =
        new ReentrantReadWriteLock(true);
    
      private static Collection<Department> departments = null;
    
      public Collection<Department> getAllDepartments()
      {
        Lock read = lock.readLock();
        read.lock();
        try {
          /* Check whether the departments are loaded. */
          if (departments == null) {
            /* If not loaded, release read-lock and acquire write-lock. */
            read.unlock();
            Lock write = lock.writeLock();
            write.lock();
            try {
              /* Recheck condition for updates by another thread. */
              if (departments == null) {
                Collection<Department> tmp = ...; // Initialize. 
                departments = Collections.unmodifiableCollection(tmp);
              }
              /* Downgrade from write-lock to read-lock. */
              read.lock();
            }
            finally {
              write.unlock();
            }
          }
          return departments;
        }
        finally {
          read.unlock();
        }
      }
    
      public void clearCache()
      {
        Lock write = lock.writeLock();
        write.lock();
        try {
          departments = null;
        }
        finally {
          write.unlock();
        }
      }
    
    }
    

    Yes, it’s messy. Sharing data between threads is hard.

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

Sidebar

Related Questions

Good morning SO, I have a class that contains the following static array: public
The problem: I have a class which contains a template method execute which calls
I have a MATLAB class which contains a reference to a java object classdef
I have a base class Node which contains a list of child nodes. Node
Let's say you have a class called Customer, which contains the following fields: UserName
I have an solution in VS 2008 which contains two class library projects and
I have a base class that declares a private non-static reference to the DataBase
if i have for example class A which contains the functions: //this is in
I have jFrame class MainForm which contains main() , placePanel(panel) and drop down menu.
I have Packet class contains virtual method and I have LogInRequest class which extends

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.