使用 BufferedWriter 和 FileWriter 将数据写入同一文件时是否需要实现同步?
我正在研究 Webmethods 集成服务器。里面有一个java服务,它采用静态java方法的形式,用于使用BufferedWriter和FileWriter将数据写入日志文件(server.log)。静态方法代码如下:
public static void writeLogFile(String message) throws ServiceException{
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("./logs/server.log", true));
bw.write(message);
bw.newLine();
bw.close();
} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
}
注:
-出于示例目的,代码已被简化。
-我无法更改 writeLogFile 方法声明和属性。这意味着,它将始终是:public static void writeLogFile。禁止这种修改:public synchronized void writeLogFile。
writeLogFile 方法有可能被不同的实例调用,因此我需要确保没有两个或多个实例同时访问相同的资源(server.log)。这意味着,如果有两个实例尝试访问 server.log,其中一个实例必须等待另一个实例完成将数据写入 server.log。
问题是: 我应该更改上面的代码吗?如果是这样,我需要做什么样的修改?我应该在java静态方法中实现“同步”吗?
@EJP:
那么,下面哪一段代码是实现synchronized的最佳代码呢?
1)
FileWriter fw = new FileWriter("./logs/server.log", true);
synchronized (fw) {
BufferedWriter bw = new BufferedWriter(fw);
bw.write(message);
bw.newLine();
bw.close();
}
2)
BufferedWriter bw = new BufferedWriter(new FileWriter("./logs/server.log", true));
synchronized(bw) {
bw.write(message);
bw.newLine();
bw.close();
}
3)
synchronized(util.class) { //yes, the class name is started with lowercase
BufferedWriter bw = new BufferedWriter(new FileWriter("./logs/server.log", true));
bw.write(message);
bw.newLine();
bw.close();
}
4) 其他意见?
谢谢。
I am working on Webmethods Integration Server. Inside there is a java service which is in form of a static java method for writing data to a log file (server.log) by using BufferedWriter and FileWriter. The static method code is like this:
public static void writeLogFile(String message) throws ServiceException{
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("./logs/server.log", true));
bw.write(message);
bw.newLine();
bw.close();
} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
}
Note:
-The code has been simplified for example purpose.
-I can't change the writeLogFile method declaration and attribute. That means, it will always be: public static void writeLogFile. This kind of modification is prohibited: public synchronized void writeLogFile.
There is a chance that the writeLogFile method can be invoked by different instances, so I need to make sure that there are no two or more instances access same resource (server.log) in same time. That means, if there are two instances try to access the server.log, one of the instances must have to wait another instance to finish writing data to the server.log.
The questions are:
Should I change the code above? If so, what kind of modification I need to do? Should I implement "synchronized" inside the java static method?
@EJP:
So, which one below is the best code to implement synchronized?
1)
FileWriter fw = new FileWriter("./logs/server.log", true);
synchronized (fw) {
BufferedWriter bw = new BufferedWriter(fw);
bw.write(message);
bw.newLine();
bw.close();
}
2)
BufferedWriter bw = new BufferedWriter(new FileWriter("./logs/server.log", true));
synchronized(bw) {
bw.write(message);
bw.newLine();
bw.close();
}
3)
synchronized(util.class) { //yes, the class name is started with lowercase
BufferedWriter bw = new BufferedWriter(new FileWriter("./logs/server.log", true));
bw.write(message);
bw.newLine();
bw.close();
}
4) Other opinion?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需使方法同步即可。出于二进制兼容性目的,它不会影响其方法签名。
Just make the method synchronized. It doesn't affect its method signature for binary compatibility purposes.
我还有另一个建议。我想同步可以被视为一个方面,并且可以使用某些 AOP 框架来实现相同的效果。这符合您不更改代码的要求。但我对此不是 100% 确定,发布了一个问题< /a> 相同。请关注其反应。
I have another suggestion. I guess synchronization can be treated as an aspect and the same can be achieved using some AOP framework. This conforms to you requirement of not changing the code. But I am not 100% sure about it and posted a question for the same. Please monitor its responses .
不。 BufferedWriter 和 FileWriter 的基类是 java.io.Writer,
它在每个写入操作上都有自己的锁。
尝试将 BufferedWriter bw 设为静态并通过静态方法引用它,因此所有写入都通过相同的 < 写入文件 顺便说一句,强>作家对象
,我猜你正在发明另一个日志库......也许你可以使用log4j或任何类型的日志库来代替
No. the base class of BufferedWriter and FileWriter is java.io.Writer,
it has it own lock on each write operation
try make the BufferedWriter bw static and ref to it by a static method ,thus all write is write to file via same Writer object
btw, i guess you are inventing yet-another-log-lib... may be you could use log4j or any kind of log lib instead