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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:04:45+00:00 2026-05-15T00:04:45+00:00

My problem Let’s say I want to hold my messages in some sort of

  • 0

My problem

Let’s say I want to hold my messages in some sort of datastructure for longpolling application:

1. "dude"
2. "where"
3. "is"
4. "my"
5. "car"

Asking for messages from index[4,5] should return:
"my","car".

Next let’s assume that after a while I would like to purge old messages because they aren’t useful anymore and I want to save memory. Let’s say after time x messages[1-3] became stale. I assume that it would be most efficient to just do the deletion once every x seconds. Next my datastructure should contain:

4. "my"
5. "car"

My solution?

I was thinking of using a concurrentskiplistset or concurrentskiplist map. Also I was thinking of deleting the old messages from inside a newSingleThreadScheduledExecutor. I would like to know how you would implement(efficiently/thread-safe) this or maybe use a library?

  • 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-15T00:04:46+00:00Added an answer on May 15, 2026 at 12:04 am

    The big concern, as I gather it, is how to let certain elements expire after a period. I had a similar requirement and I created a message class that implemented the Delayed Interface. This class held everything I needed for a message and (through the Delayed interface) told me when it has expired.

    I used instances of this object within a concurrent collection, you could use a ConcurrentMap because it will allow you to key those objects with an integer key.

    I reaped the collection once every so often, removing items whose delay has passed. We test for expiration by using the getDelay method of the Delayed interface:

    message.getDelay(TimeUnit.MILLISECONDS);
    

    I used a normal thread that would sleep for a period then reap the expired items. In my requirements it wasn’t important that the items be removed as soon as their delay had expired. It seems that you have a similar flexibility.

    If you needed to remove items as soon as their delay expired, then instead of sleeping a set period in your reaping thread, you would sleep for the delay of the message that will expire first.

    Here’s my delayed message class:

    class DelayedMessage implements Delayed {
    
        long endOfDelay;
        Date requestTime;
        String message;
    
        public DelayedMessage(String m, int delay) {
            requestTime = new Date();
            endOfDelay = System.currentTimeMillis()
                    + delay;
            this.message = m;
        }
    
        public long getDelay(TimeUnit unit) {
            long delay = unit.convert(
                    endOfDelay - System.currentTimeMillis(),
                    TimeUnit.MILLISECONDS);
            return delay;
        }
    
        public int compareTo(Delayed o) {
            DelayedMessage that = (DelayedMessage) o;
            if (this.endOfDelay < that.endOfDelay) {
                return -1;
    
            }
            if (this.endOfDelay > that.endOfDelay) {
                return 1;
            }
            return this.requestTime.compareTo(that.requestTime);
        }
    
        @Override
        public String toString() {
            return message;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so I have a problem: Let's say I have Messages and Users and the
I would like to have some suggestions for the following problem: Let's say you
The problem: let's say I have two tables Client , and Product , in
The underlying problem - let's say my documents have categories and timestamps . If
Here is my problem: I have an array of model class(Let's say, 'addressModel' with
Let's say I'm programming in Java or Python or C++ for a simple problem,
This is a design problem I am facing. Let's say I have a cars
Before explaining my bash problem let me give you some context: I am writing
I am stuck with following monad problem: Let's say I have a standard monad
I'm trying to come up with a reasonable algorithm for this problem: Let's say

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.