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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:07:40+00:00 2026-05-25T18:07:40+00:00

I am looking for a concurrent Set with expiration functionality for a Java 1.5

  • 0

I am looking for a concurrent Set with expiration functionality for a Java 1.5 application. It would be used as a simple way to store / cache names (i.e. String values) that expire after a certain time.

The problem I’m trying to solve is that two threads should not be able to use the same name value within a certain time (so this is sort of a blacklist ensuring the same “name”, which is something like a message reference, can’t be reused by another thread until a certain time period has passed). I do not control name generation myself, so there’s nothing I can do about the actual names / strings to enforce uniqueness, it should rather be seen as a throttling / limiting mechanism to prevent the same name to be used more than once per second.

Example:
Thread #1 does cache.add("unique_string, 1) which stores the name “unique_string” for 1 second.
If any thread is looking for “unique_string” by doing e.g. cache.get("unique_string") within 1 second it will get a positive response (item exists), but after that the item should be expired and removed from the set.

The container would at times handle 50-100 inserts / reads per second.

I have really been looking around at different solutions but am not finding anything that I feel really suites my needs. It feels like an easy problem, but all solutions I find are way too complex or overkill.

A simple idea would be to have a ConcurrentHashMap object with key set to “name” and value to the expiration time then a thread running every second and removing all elements whose value (expiration time) has passed, but I’m not sure how efficient that would be? Is there not a simpler solution I’m missing?

  • 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-25T18:07:40+00:00Added an answer on May 25, 2026 at 6:07 pm

    How about creating a Map where the item expires using a thread executor

    //Declare your Map and executor service
    final Map<String, ScheduledFuture<String>> cacheNames = new HashMap<String, ScheduledFuture<String>>();
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    

    You can then have a method that adds the cache name to your collection which will remove it after it has expired, in this example its one second. I know it seems like quite a bit of code but it can be quite an elegant solution in just a couple of methods.

    ScheduledFuture<String> task = executorService.schedule(new Callable<String>() {
      @Override
      public String call() {
        cacheNames.remove("unique_string");
        return "unique_string";
      }
    }, 1, TimeUnit.SECONDS);
    cacheNames.put("unique_string", task);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for an implementation of java.util.Set with the following features: Should be concurrent
I'm looking for a way of getting a concurrent collection in C# or at
I was looking at the source code of java.uti.concurrent.locks.AbstractQueuedSynchronizer and the acquire() method looks
In the java world there is a set of classes optimized for concurrent tasks.
I am reading Java Concurrency in practice and looking at the example code on
I am reading Java Concurrency in practice and looking at the example code on
I'm currently working on some concurrent code that would appear to have a few
I'm writing an application that will have to be able to handle many concurrent
I've been looking at the problem of writing a concurrent Multimap , and I
I need to confirm a theory. I'm learning JSP/Java. After looking at an existing

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.