This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
    Bookmark Topic Watch Topic
  • New Topic

Singleton and writing to a common file

 
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.
 
    Bookmark Topic Watch Topic
  • New Topic