我是否需要使用BufferedWriter和FileWriter在将数据写入同一文件时实现同步? [英] Do I need to implement synchronized on writing data to a same file by using BufferedWriter and FileWriter?

查看:163
本文介绍了我是否需要使用BufferedWriter和FileWriter在将数据写入同一文件时实现同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Webmethods Integration Server.内部有一个Java服务,该服务采用静态Java方法的形式,用于通过使用BufferedWriter和FileWriter将数据写入日志文件(server.log).静态方法代码如下:

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());
    }
}

注意:
-出于示例目的,已简化了代码.
-我无法更改writeLogFile方法的声明和属性.这意味着,它将始终是:public static void writeLogFile.禁止这种修改:公共同步void writeLogFile.

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.

有一个机会可以由不同的实例调用writeLogFile方法,因此我需要确保没有两个或更多实例同时访问同一资源(server.log).这意味着,如果有两个实例尝试访问server.log,则其中一个实例必须必须等待另一个实例才能将数据写入server.log.

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.

问题是: 我应该更改上面的代码吗?如果是这样,我需要做什么样的修改?我应该在java静态方法中实现已同步"吗?

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:
那么,下面哪个是实现同步的最佳代码?

@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)其他意见?

谢谢.

推荐答案

只需使方法同步即可.出于二进制兼容性目的,它不会影响其方法签名.

Just make the method synchronized. It doesn't affect its method signature for binary compatibility purposes.

这篇关于我是否需要使用BufferedWriter和FileWriter在将数据写入同一文件时实现同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆