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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:34:08+00:00 2026-06-16T16:34:08+00:00

I am on my way learning multi-thread programming with Java. Here is a confusion

  • 0

I am on my way learning multi-thread programming with Java. Here is a confusion I got.

class Cache<K, V> {
private ConcurrentMap<K, V> cache;
private ConcurrentLinkedQueue<K> lru;

public Cache () {
    // initiate cache and lru
}

public put (K key, V value) {
    // some pre-processing
    synchronized (this) {
        cache.put(key, value);
        lru.add(key);
    }
    // some post-processing
}

}

Here is some very simple cache with a least recently used record (lru). Obviously I need to make these two operations atomic. Otherwise it’s very likely the state of cache and lru would be different.

Now assume I want to have a timer task to clean the cache, say it will clean half the cache. My question is does my code above ensure these two operations (put cache and add lru) appear atomic to the clean task? Can I do something like below:

class CleanTask {
    Cache cache;   // the reference of Cache
    public void run () {
        // some pre-processing
        for (int i = 0; i < n; i++) {   // Just suppose I need remove n element
            synchronized (XXX) {
                cache.getCache().remove(cache.getLru().poll());
            }
        }
    }
}

And what should I put in XXX?

Thanks a lot!!!

  • 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-16T16:34:10+00:00Added an answer on June 16, 2026 at 4:34 pm

    My question is does my code above ensure these two operations (put cache and add lru) appear atomic to the clean task?

    yes (assuming cleaning task is in different thread)

    And what should I put in XXX?

    the same cache object for example: synchronized (cache) { a the synchronization should happen on the same lock (object)

    you can also explore other atomic classes in java if they could be useful in your case: atomic package

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

Sidebar

Related Questions

I am on my way learning Java multithread programming. I have a following logic:
I was planning on learning a way to create my own programming language and
I'm learning socket programming (in python) and I was wondering what the best/typical way
I'm new to C(coming from Java, so I'm not learning programming for the first
I'm learning Java and was able to do a bit of multi-threading with my
I´m still learning my way around MVC programming and have one question for you
Started learning java a week ago and also decided learning the right way to
In my way of learning drupal, I got stuck in this issue. I have
As a way of learning Java, I'm writing this little application for grade schoolers
I'm working my way through learning writing a COM control. I've got an example

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.