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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:59:07+00:00 2026-06-03T18:59:07+00:00

I have several threads which need to write to two different text files. So

  • 0

I have several threads which need to write to two different text files. So far I have this code:

public class Logger {

    public static void printToGameLog(String value){
        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("GameLog.txt", true), "utf-8"));
            synchronized(writer){
                writer.write(outputString + "\r\n");
            }
        } catch (IOException ex){
            System.out.println("cannot create log file");
        } 
    }


    public static void printToServerLog(String value){
        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("serverLog.txt", true), "utf-8"));
            synchronized(writer){
                writer.write(outputString + "\r\n");
            }
        } catch (IOException ex){
            System.out.println("cannot create log file");
        }
    }
}

Is this an acceptable way of ensuring no more than one thread is writing to the same file at the same time?

If a thread calls one of these methods and enters the sync block, then what happens if another thread comes along and tries to execute the same method. When it tries to use the local variable writer, will it try and obtain the same object that has been locked by the other thread and therefore block? I would have thought that it would simply create its own separate variable, which would mean I should make writer a static class variable instead?

  • 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-03T18:59:11+00:00Added an answer on June 3, 2026 at 6:59 pm

    Since there are separate log files, I don’t see why you need to have class-level synchronization. Seems like a needless bottleneck. I’d provide sync for each method separately (since it’s fine for them to hit separate files simultaneously):

    public class Logger
    {
        private static final Object GAME_LOG_LOCK = new Object();
        private static final Object SERVER_LOG_LOCK = new Object();
    
        public static void printToGameLog(String value){
            synchronized (GAME_LOG_LOCK) {
                Writer writer = null;
                try {
                    writer = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream("GameLog.txt", true), "utf-8"));
                    writer.write(outputString + "\r\n");
                } catch (IOException ex){
                    System.out.println("cannot create log file");
                } 
            }
        }
    
        public static void printToServerLog(String value){
            synchronized (SERVER_LOG_LOCK) {   
                Writer writer = null;
                try {
                    writer = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream("serverLog.txt", true), "utf-8"));
                    writer.write(outputString + "\r\n");
                } catch (IOException ex){
                      System.out.println("cannot create log file");
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write C++ API, which consists of several exported C++ classes exposed
I have several threads which act as backup for the main one spending most
I have a main thread which should process data coming from several worker threads.
I have a situation which involves several threads simultaneously populating a database with data
I have a problem which requires me to parse several log files from a
Where is the TickCount() call? I have seen several threads on the web that
I have a queue structure that is being used by several pthreads. The threads
I have several textboxes where users can enter information into them. This can include
I have several python processes which monitor and act upon physical IO. E.g. shut
i saw some info regarding this question in several threads but non suited my

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.