Synchronize file object

Viewed 8887

From what I know and researched, the synchronized keyword in Java lets synchronize a method or code block statement to handle multi-threaded access. If I want to lock a file for writing purposes on a multi-threaded environment, I must should use the classes in the Java NIO package to get the best results. Yesterday, I come up with a question about handling a shared servlet for file I/O operations, and BalusC comments are good to help with the solution, but the code in this answer confuses me. I'm not asking community "burn that post" or "let's downvote him" (note: I haven't downvoted it or anything, and I have nothing against the answer), I'm asking for an explanation if the code fragment can be considered a good practice

private static File theFile = new File("theonetoopen.txt");

private void someImportantIOMethod(Object stuff){
    /*
        This is the line that confuses me. You can use any object as a lock, but
        is good to use a File object for this purpose?
    */
    synchronized(theFile) {
        //Your file output writing code here.
    }
}
4 Answers
Related