This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
    Bookmark Topic Watch Topic
  • New Topic

Singleton and writing to a common file  RSS feed

 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Report post to moderator
This is a follow-up to my previous post about "thread safe and writing to a common file". I changed the code to include singleton. Here is sample

public class LogWriter {

private static LogWriter lw = new LogWriter();
private LogWriter() {}
public static synchronized LogWriter getLogWriter() {
if(lw == null) lw = new LogWriter();
return lw;
}

private synchronized writeLog() {
// open file
// write file
// close file
}
}


Does this work ? I guess in this case I don't need to put "synchronized" keyword for an "object" inside the "writeLog()" method, right ? This is because I ensure only one static global instance is created. Please help me verify it.

thanks lot.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
Let's not have five different conversations about this, please. I'm closing this thread; follow-ups can go here. Thank you.
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!