从Java中的多个线程写入FileOutputStream

时间:2011-12-07 20:45:13

标签: java file-io

从多个线程的Java write对象上调用FileOutputStream是否安全?输出是否会正确序列化?

澄清:

在我的例子中,类记录器包含一个FileOutputStream引用,多个线程可以调用logger write,它格式化输出并调用FileOutputStream write。

我是否应该同步我的记录器写入方法以保证来自多个线程的消息不会混合?

4 个答案:

答案 0 :(得分:5)

以下是使用java nio FileChannel的同步记录器的简单实现。 在此示例中,日志消息限制为1024个字节。您可以通过更改BUFFER_SIZE值来调整日志消息长度。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;

/**
 *  The MyLogger class abstracts the writing of log messages to a file.
 *  This is a synchronized implementation due to the usage of java.nio.channels.FileChannel 
 *  which is used to write log messages to the log file.
 *  
 *  The MyLogger class maintains a HashMap of MyLogger instances per log file.  
 *  The Key is the MD5 hash of the log file path and the Value is the MyLogger instance for that log file.
 *
 */
public final class MyLogger {
    private static final int BUFFER_SIZE = 1024;
    private static final int DIGEST_BASE_RADIX = 16;
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    private static HashMap<String, MyLogger> sLoggerMap;

    private FileChannel mLogOutputChannel;
    private ByteBuffer mByteBuffer;
    private String mLogDir;
    private String mLogFileName;

    /**
     * Private constructor which creates our log dir and log file if they do not already already exist. 
     * If the log file exists, then it is opened in append mode.
     * 
     * @param logDir
     *            The dir where the log file resides
     * @param logFileName
     *            The file name of the log file
     * @throws IOException
     *             Thrown if the file could not be created or opened for writing.
     */
    private MyLogger(String logDir, String logFileName) throws IOException {
        mLogDir = logDir;
        mLogFileName = logFileName;

        // create the log dir and log file if they do not exist
        FileOutputStream logFile;
        new File(mLogDir).mkdirs();

        final String logFilePath = mLogDir + File.separatorChar + mLogFileName;
        final File f = new File(logFilePath);
        if(!f.exists()) {
            f.createNewFile();
        }
        logFile = new FileOutputStream(logFilePath, true);

        // set up our output channel and byte buffer  
        mLogOutputChannel = logFile.getChannel();
        mByteBuffer = ByteBuffer.allocate(BUFFER_SIZE);
    }

    /**
     * Writes the given log message to the log file that is represented by this MyLogger instance. 
     * If the log message could not be written to the log file an error is logged in the System log.
     * 
     * @param logMessage
     *            The log message to write to the log file.
     */
    public void log(String logMessage) {

        // write the log message to the log file
        if (mLogOutputChannel != null) {
            mByteBuffer.put(logMessage.getBytes());
            mByteBuffer.put(LINE_SEPARATOR.getBytes());
            mByteBuffer.flip();
            try {
                mLogOutputChannel.write(mByteBuffer);
                // ensure that the data we just wrote to the log file is pushed to the disk right away
                mLogOutputChannel.force(true);
            } catch (IOException e) {
                // Could not write to log file output channel
                e.printStackTrace();
                return;
            }
        }

        if(mByteBuffer != null) {
            mByteBuffer.clear();
        }
    }

    /**
     * Get an instance of the MyLogger for the given log file. Passing in the same logDir and logFileName will result in the same MyLogger instance being returned.
     * 
     * @param logDir
     *            The directory path where the log file resides. Cannot be empty or null.
     * @param logFileName
     *            The name of the log file Cannot be empty or null.
     * @return The instance of the MyLogger representing the given log file. Null is returned if either logDir or logFilename is null or empty string.
     * @throws IOException
     *             Thrown if the file could not be created or opened for writing.
     */
    public static MyLogger getLog(String logDir, String logFileName) throws IOException {
        if(logDir == null || logFileName == null || logDir.isEmpty() || logFileName.isEmpty()) {
            return null;
        }

        if(sLoggerMap == null) {
            sLoggerMap = new HashMap<String, MyLogger>();
        }

        final String logFilePathHash = getHash(logDir + File.separatorChar + logFileName);
        if(!sLoggerMap.containsKey(logFilePathHash)) {
            sLoggerMap.put(logFilePathHash, new MyLogger(logDir, logFileName));
        }

        return sLoggerMap.get(logFilePathHash);
    }

    /**
     * Utility method for generating an MD5 hash from the given string.
     * 
     * @param path
     *            The file path to our log file
     * @return An MD5 hash of the log file path. If an MD5 hash could not be generated, the path string is returned.
     */
    private static String getHash(String path) {
        try {
            final MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(path.getBytes());
            return new BigInteger(digest.digest()).toString(DIGEST_BASE_RADIX);
        } catch (NoSuchAlgorithmException ex) {
            // this should never happen, but just to make sure return the path string
            return path;
        }
    }
}

这就是你如何使用它:

MyLogger aLogger = MyLogger.getLog("/path/to/log/dir", "logFilename");
aLogger.log("my log message");

答案 1 :(得分:4)

write-mode中无法多次打开文件,因此答案是否定的。

看到你的编辑后,是的,你应该在你的记录器中引入同步,以确保一次只能由一个线程访问流。只是一个建议,你为什么不去Log4J?它已经处理了你的用例。

答案 2 :(得分:2)

没有。 Java不支持从多个线程流式传输到相同的流。

如果您想使用线程流,请查看此站点:http://lifeinide.com/post/2011-05-25-threaded-iostreams-in-java/

他解释得很好,并为ThreadedOutputStream提供了一些示例代码,可以做你想要的。

答案 3 :(得分:1)

如果要保持排序(即输出流中的消息1出现在消息2之前),则必须锁定流。这反过来又降低了并发性。 (所有线程都将在lock的/ semaphore的队列中排队,然后等待流可供他们使用)

如果您只对同时写入流并且不关心排序感兴趣,则可以为每个线程提供缓冲区。每个线程都写入自己的缓冲区。当缓冲区已满时,它会获取流上的锁(可能涉及等待锁)并将其内容清空到流中。

编辑: 我刚刚意识到,如果你关心排序并且仍然想要多线程,如果你也在unix格式的输出流中写出时间(如长)。将流刷新到其他容器后,可以根据时间对内容进行排序,您应该有一个有序的文件。