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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:56:06+00:00 2026-05-19T01:56:06+00:00

I find myself again and again faced with a similar problem: there’s some piece

  • 0

I find myself again and again faced with a similar problem: there’s some piece of code that processes data as it arrives from the user/network/produces of some sort. For efficiency reasons, I don’t want to call flush() or commit() on every piece of data that I receive, but only occasionally.

I usually come up with code like this:

class Processor {
    private final static MAX_SAVE_PERIOD = 60000;
    private final static MIN_SAVE_PERIOD = 20000;

    private final static int MAX_BUFFER = 10000;
    Arraylist<Data> dataBuffer = new Arraylist<Data>();

    private long lastSave = 0;

    public Saver() {
        new Timer().schedule(new TimerTask() {
            periodicSave();
        }, MAX_SAVE_PERIOD, MAX_SAVE_PERIOD);
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            public void run() {
                periodicSave();
            }
        }));
    }

    public synchronized void processData(Data data) {
        dataBuffer.add(data);
        if(dataBuffer.size() >= MAX_BUFFER) {
            saveData();
        }
    }

    private synchronzied void periodicSave() {
        if(!dataBuffer.isEmpty()) {
            saveData();
        }
    }

    private void saveData() {
        if (System.currentTimeMillis() - lastSave < MIN_SAVE_PERIOD) return;

        ...        

        lastSave = System.currentTimeMillis();
    }
}

I get the distinct feeling that I’m reinventing the wheel every time I write this, and what’s more, I keep changing stuff every time I write this kind of code, depending if the various parts make sense in the specific context.

It seems to me that this is a very common pattern, but I don’t remember seeing it named or implemented as a library utility. As long as I have to implement this myself, I keep facing analysis paralysis whenever I reimplement it. Please help me!

UPDATE: After I wrote this, I realized that I haven’t accounted for flushing the buffer before JVM shutdown, so I added a shutdown hook in the constructor. Now I’ve realized that this code will still not work properly if the shutdown happens less than MIN_SAVE_PERIOD milliseconds after the last save, so I should probably refactor saveData. It’s happening again.

  • 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-19T01:56:07+00:00Added an answer on May 19, 2026 at 1:56 am

    Your code already says it: it’s called buffering.

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

Sidebar

Related Questions

It appears after much searching that there seems to be a common problem when
I find myself wanting to write a routine that will operate on both volatile
Ok, I find myself doing this often. I'll attach ids to my tables and
I find myself needing to use Perl more and more since PHP's regular expressions
Again, I am new to JPA & Hibernation. I am trying to use JPA
I've been struggling to find a decent RTP stack implementation for Java . JMF
I am working on a game in C# (either 2.0 or 3.5 havn't decided
I'm building a membership system keyed by email address. I.e. Email/password. As I do
When using Applesoft Basic on the Apple 2 with an 80 column card, is
Can anyone recommend a fast way to sort the contents of a text file,

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.