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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:20:59+00:00 2026-06-14T12:20:59+00:00

Every time a page is requested on my website, I log some details with

  • 0

Every time a page is requested on my website, I log some details with a service call like this:

logService.logPageRequest(session, request, contentId);

This service call makes use of a static FileUtils (my own) method to do the actual update on the file.

public static void appendToTextFile(String string, String fileRootDirectory, String subdirectory, String filename, boolean startOnNewLine) throws Exception {
    // code irrelevant to the question removed

    String filePath=fileDirectory+"/"+filename;
    File file=new File(filePath);
    if(file.exists()) {
        FileWriter out = new FileWriter(filePath, true);
        if(startOnNewLine) {
            out.write("\r\n");
        }
        out.write(string);
        out.close();
     } else {
         FileWriter out = new FileWriter(filePath);
         out.write(string);
         out.close();
}

Every so often (let’s say scheduled every 15 minutes), I execute another service which renames this file and starts processing it.

I’d like to understand how I can go about ensuring that both concurrent writes are safe and rename is not executed while a write is in the process. I guess the answer will be some form of synchronization.

  • 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-14T12:21:00+00:00Added an answer on June 14, 2026 at 12:21 pm

    The two services need to share a synchronization object: it can be anything (even new byte[1]) but it must be the one and same instance, passed into your “services”. Whenever there is a critical section, which means part of a code which wants to operate on a shared resource, it needs to be wrapped in

    synchronized(theInstanceOfSynchronizationObject) {
      // ... your code for critical section
    }
    

    The rule of good locking is to know deterministically what are you locking, what is exactly the shared resource. What will kill you (and introduce deadlocks) is random adding of synchronized blocks in hope of “solving”. In your particular example, it’s most probably the actions on file: just actions, not the operations on filename strings, neither the instantiation of File : you can have many File objects pointing to the same file on disk and it’s not a critical operation, as long as it does not touch the disk yet. So i would say

    File file=new File(filePath);
    synchronized(theInstanceOfSynchronizationObject) {
      if(file.exists()) {
      } else {
      }
    }
    

    Similarly, protect the File actions in your other service. Keep in mind that it’s not File object you are protecting, but the file itself.

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

Sidebar

Related Questions

In web applications like phpBB, when every time a new page is requested, is
This is my problem. Every time I load the page, I get an alert
I read some website development materials on the Web and every time a person
On my site I need to request a date/time every page reload. The reason
Is it executed / read-by-the-server every time a page on the site is loaded?
In Visual studios, is the class recalled every time the page refreshes? I have
Every time I open a page I want to get the currently active project
The speed with these libraries results in the page blinking every time I reload
NHibernate_reference.pdf, page 26: Note that ILifecycle.OnUpdate() is not called every time the object's persistent
When using Visual Studio's built in web server, every time I make a page

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.