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

  • Home
  • SEARCH
  • 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 8662873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:50:24+00:00 2026-06-12T16:50:24+00:00

I am developing an application in which I continuously receive messages. I am storing

  • 0

I am developing an application in which I continuously receive messages. I am storing these messages in an in-memory data structure (say List). I want to write those messages to a file but only after list size reaches some threshold value say 100 messages (perform batch processing on the messages).
One way is I can simply check the list size after every message is received and call a function to write messages to a file if Threshold value is reached. But the problem with this approach is:

  1. Calling function may need to wait indefinitely until all the messages are written to the file
  2. The incoming messages may be lost in the process or might need to wait for getting stored in the List.

Other way could be to spawn a new thread, which will write messages to a file independently. But when I pass the list (containing messages) to the thread for performing write operation, it gets updated with new messages which are continuously coming in. As a result newly arrived messages also gets written to the file which is not expected.

This should not happen as I am intending the new messages to be written in the next batch.

Can someone suggest me a solution for this requirement, or any improvements in the above approach that can solve my issues.

  • 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-12T16:50:25+00:00Added an answer on June 12, 2026 at 4:50 pm

    It’s important to understand that you never pass an object around in Java – only ever a reference (or a primitive value).

    Options:

    • Create a copy of the list, and pass a reference to that copy to your new thread
    • Use a producer/consumer queue, so your “producing” thread only ever adds values to the queue, and your consumer thread only ever takes items from the queue to write them to disk. You’ll want to think about how large you want the queue to potentially get before it stops accepting more entries, of course.

    I’d recommend the latter approach, using the classes in the java.util.concurrent package to implement it; particularly BlockingQueue<E> implementations.

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

Sidebar

Related Questions

i am developing application which send and receive data between two computers but there
I im developing a application in which I continuously download large amount of data.
I am developing an application which will read and write data to gsm modem.
I`m developing an application which receives data from my GPS device like coordinates, speed
I'm developing an application which needs to store large amounts of data. I cannot
I am developing an application which generates barcodes, I want to print them but
I'm developing an application which will have these classes: class Shortcut { public string
I am developing an application, in which I want to redirect the output (progress
I'm developing an application which import a lot of data, in some paralells thread.
I'm developing an application which loads lots of data (like from csv). I'm creating

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.