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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:25:11+00:00 2026-06-04T07:25:11+00:00

I have many threads that concurrently read the same file(entirely about 100M), and only

  • 0

I have many threads that concurrently read the same file(entirely about 100M), and only one thread to update the file. I want to map the file in memory to reduce FILE I/O. How can this be done in Java?

I basically have considered the following 2 methods:

  1. with byte array to store the file, and each time create ByteArrayInputStream to read the buffer when multi-thread read.
  2. with NIO to get one file channel, synchronized the channel to read from the MappedByteBuffer for multi-thread read.

I’m not sure whether the methods should work. Please help to give some hint if there is a better solution.

  • 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-04T07:25:12+00:00Added an answer on June 4, 2026 at 7:25 am

    Use NIO with each thread creating its own mapping and reading the data in its own private buffer. Keep the private buffer size optimal. The OS reads the file in its file cache in pages and the pages are read into the private buffers. If same regions are read by multiple threads then the data would be read from the same pages in the file cache saving some file i/o cycles. Below is a small diagram to indicate this. Hope it helps to understand better.

    memory mapped file io

    With reference to the diagram above, below is some explanation. A region of the file is mapped to memory. Creating a mapping is just a logical marking to say that you want to read from a particular portion of a file. Once the mapping is created the mapped region is ready to be read. When you start reading, the OS fetches the file data into its pages in the file cache. The region could be mapped to one or more pages. Now, you read the pages into your own private buffer (multiple pages at a time to optimize). Some other thread could be reading the same region as the first one, so it also reads the same pages into its private buffer. Note that this time the read happens from the file cache without page faults. After you have processed your private buffer, you request to read further. Note that you are reading a portion of your mapping into your private buffer at a time. Your file could be 100MB and you map a 10MB portion to memory; and you coud have 40KB private buffer and you read 40KB out of 10MB first. Then request for next 40KB and so on. The OS checks if the data you want to read is already fetched into the cache. If not, a page fault occurs and the OS fetches the data requested in to the pages. Again this data can be shared if multiple thread request to read the same region. You can very well use the file cache itself for reading instead of creating your own private buffer. But, this can lead to multiple page faults if the file is concurrently read multiple times across multiple regions. So it this case it better to have a private buffer of optimal size.

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

Sidebar

Related Questions

I have a black box of code that contains many threads. There is no
Let's say I have to read from a directory that has many large XML
I have many threads using the below interface ServerMessage If many threads call the
I have come across many ad hoc implementations of thread class in C++, but
I have many tables that use Lookup/Enum references for most of their column values.
I have many forms that use AJAX (w/ jQuery) for validation and data submission.
I have many many small images that are displayed on a user's profile and
I'm writing an application that will have to be able to handle many concurrent
I have a Java application that has a fixed thread pool of fifteen, the
I have to print many formatted decimal values in many threads in parallel. To

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.