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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:03:27+00:00 2026-05-11T12:03:27+00:00

I have two independant threads F1 and F2 (to be precise, two instances of

  • 0

I have two independant threads F1 and F2 (to be precise, two instances of java.util.concurrent.FutureTask) that are running in parallel.

F1 do some processing, and then copy the result in a XML file. Then, it repeats these steps until it has nothing to do (many XML files are created). F2 looks in the F1 output directory, and take one file, parse it, and execute some processing on it.

This works quite pretty well, except that sometimes, F2 gets truncated XML data from the file. I mean by that an incomplete XML, where some XML node are not present. The problem is that it is not always reproductible, and the files that are truncated are not always the same. Because of that, I am thinking that while F1 is writing one file on the disk, F2 is trying to read the same file. That’s why sometimes I get this kind of error.

My question: I am wondering if there is some mechanism that locks (even for reading) the file F1 is currently writing until it has completely finished to write it on the disk, so F2 will not be able to read it until the file is unlocked. Or any other way to solve my issue will be welcome !

F1 is writing the file this way:

try {     file = new File('some-file.xml');     FileUtils.writeStringToFile(file, xmlDataAsString); } catch (IOException ioe) {     LOGGER.error('Error occurred while storing the XML in a file.', ioe); } 

F2 is reading the file this way:

private File getNextFileToMap() {     File path = getPath(); // Returns the directory where F1 stores the results...     File[] files = path.listFiles(new FilenameFilter() {         public boolean accept(File file, String name) {             return name.toLowerCase().endsWith('.xml');         }     });     if (files.length > 0) {         return files[0];     }     return null; }  // Somewhere in my main method of F2 ... f = getNextFileToMap(); Node xmlNode = null; try {     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();     DocumentBuilder builder = factory.newDocumentBuilder();     Document doc = builder.parse(f);     if (doc != null) {         xmlNode = doc.getDocumentElement();     } } catch (Exception e) {     LOGGER.error('Error while getting the XML from the file ' + f.getAbsolutePath(), e); } 
  • 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. 2026-05-11T12:03:27+00:00Added an answer on May 11, 2026 at 12:03 pm

    Since you’re already filtering for .xml files in F2, have F1 output to a .temp file, then rename it to .xml as a final step. That way, F2 will ignore the file F1 is making until F1 is completely done with it.

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

Sidebar

Related Questions

I am programming multi-threaded app. I have two threads. On is for transferring some
I have two files independant on each other. Let's just call it Class1 and
I'm going to have two independent programs (using SqlAlchemy / ORM / Declarative) that
I have a project that uses two independent Android libraries. Each of them contains
Have two folders with approx. 150 java property files. In a shell script, how
Let's say I have two variables, protected_var1 and protected_var2 . Let's further assume that
So i have two classes that are independent of eachother, but both are related
I have two Eclipse projects: one that depends upon a certain library (android.jar) and
Looks like I have messed up with Java Threads/OS Threads and Interpreted language. Before
I have a XAML form that I would like two independent features threaded out

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.