本文介绍了使用bufferwriter由文件写入器线程初始化的文件写入是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是线程安全的吗?以及为什么?

It is thread safe or not? and if so why;

这是我的代码的一部分

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tempFile, true));
bufferedWriter.write(auditParameters.toString());
bufferedWriter.newLine();
bufferedWriter.flush();
bufferedWriter.close();

推荐答案

bufferedWriter.write是同步的,与flush相同,因此从不同线程使用相同的bufferedWriter是线程安全的.我使用了以下测试程序:

bufferedWriter.write is synchronized, same as flush so using the same bufferedWriter from different threads is thread safe. I used the following testprogram:

public abstract class MultiThreadedOneInstanceTemplate  implements Runnable {

private AtomicInteger threadCount = new AtomicInteger();
private static BufferedWriter bufferedWriter;

public void test() throws Exception
{
    for(int i = 0; i < 8 ;i++)
    {
        Thread thread = new Thread(this, "First Test Thread " + i);
        this.threadCount.incrementAndGet();
        thread.start();
    }


    while( this.threadCount.get() > 0 )
    {
        Thread.sleep(1000);
    }
    Thread.sleep(10 * 1000);
}


public void run()
{
    exec();
    threadCount.decrementAndGet();
}


private static synchronized BufferedWriter getWriter()
{
    try{
        if( bufferedWriter == null )
        {
            bufferedWriter =  new BufferedWriter(new FileWriter("C:\\temp\\test.txt", true)); 
        }

        return bufferedWriter;
    }
    catch(Exception e)
    {
        throw new RuntimeException(e);
    }   
}
public void exec()
{
    try{
     getWriter().write("rrssds");
     getWriter().flush();

    }
    catch(Exception e)
    {
        throw new RuntimeException(e);
    }   
}
}

http://vmlens.com 生成了以下跟踪信息:

And http://vmlens.com generated the following trace:

com/anarsoft/agent/regression/MultiThreadedOneInstanceTemplate.run  
com/anarsoft/agent/regression/TestFileWriter.exec  
com/anarsoft/agent/regression/TestFileWriter.getWriter monitor enter (-5)
, read(com/anarsoft/agent/regression/TestFileWriter.bufferedWriter)
monitor exit ()
java/io/Writer.write  
java/io/BufferedWriter.write , read(java/io/BufferedWriter.lock)
monitor enter (1138060695)
java/io/BufferedWriter.ensureOpen 
, read(java/io/BufferedWriter.out),         read(java/io    /BufferedWriter.cb), write(java/io/BufferedWriter.nextChar), read(java/io/BufferedWriter.nChars)
monitor exit ()
com/anarsoft/agent/regression/TestFileWriter.getWriter 
monitor enter (-5)
, read(com/anarsoft/agent/regression/TestFileWriter.bufferedWriter)
monitor exit ()
java/io/BufferedWriter.flush , read(java/io/BufferedWriter.lock)
monitor enter (1138060695)
java/io/BufferedWriter.flushBuffer , read(java/io/BufferedWriter.lock)
monitor enter (1138060695)
java/io/BufferedWriter.ensureOpen 
, read(java/io/BufferedWriter.out),     read(java/io    /BufferedWriter.cb),     
write(java/nio/charset/CharsetEncoder.state), write(java/io/BufferedWriter.nextChar), read(java/nio/Buffer.limit), read(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
java/io/OutputStreamWriter.write  
java/nio/CharBuffer.wrap  
java/nio/HeapCharBuffer.<init>  
java/nio/CharBuffer.<init>  
java/nio/Buffer.<init>  
java/nio/Buffer.limit  
java/nio/Buffer.position  
java/nio/charset/CharsetEncoder.encode , read(java/io/BufferedWriter.out),     read(java/io/BufferedWriter.cb), write(java/nio/charset/CharsetEncoder.state), write(java/io/BufferedWriter.nextChar), read(java/nio/Buffer.limit), read(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
java/nio/CharBuffer.array  
java/nio/CharBuffer.arrayOffset  
java/nio/CharBuffer.arrayOffset  
java/nio/ByteBuffer.array , read(java/io/BufferedWriter.out), read(java/io /BufferedWriter.cb), write(java/nio/charset/CharsetEncoder.state), write(java/io/BufferedWriter.nextChar), read(java/nio/Buffer.limit), read(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
java/nio/ByteBuffer.arrayOffset 
, read(java/io/BufferedWriter.out), read(java/io/BufferedWriter.cb),    write(java/nio/charset/CharsetEncoder.state), write(java/io/BufferedWriter.nextChar), read(java/nio/Buffer.limit), read(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
java/nio/ByteBuffer.arrayOffset 
, read(java/io/BufferedWriter.out), read(java/io/BufferedWriter.cb), write(java/nio/charset/CharsetEncoder.state), write(java/io/BufferedWriter.nextChar), read(java/nio/Buffer.limit), read(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
java/nio/CharBuffer.arrayOffset  
java/nio/Buffer.position  
java/nio/ByteBuffer.arrayOffset , read(java/io/BufferedWriter.out),     read(java/io/BufferedWriter.cb), write(java/nio/charset/CharsetEncoder.state), write(java/io/BufferedWriter.nextChar), read(java/nio/Buffer.limit), read(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
java/nio/Buffer.position , read(java/io/BufferedWriter.out), read(java/io/BufferedWriter.cb), write(java/nio/charset/CharsetEncoder.state), write(java/io/BufferedWriter.nextChar), read(java/nio/Buffer.limit), read(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
monitor exit ()
, read(java/io/BufferedWriter.out), write(java/nio/Buffer.limit), read(java/nio/Buffer.capacity), write(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
 java/io/OutputStreamWriter.flush , read(java/io/BufferedWriter.out),     write(java/nio/Buffer.limit), read(java/nio/Buffer.capacity), write(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
 java/nio/ByteBuffer.array , read(java/io/BufferedWriter.out), write(java/nio/Buffer.limit), read(java/nio/Buffer.capacity), write(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
java/nio/ByteBuffer.arrayOffset , read(java/io/BufferedWriter.out), write(java/nio/Buffer.limit), read(java/nio/Buffer.capacity), write(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
java/io/FileOutputStream.write , read(java/io/BufferedWriter.out),     write(java/nio/Buffer.limit), read(java/nio/Buffer.capacity), write(java/nio/Buffer.mark), read(java/nio/ByteBuffer.isReadOnly), write(java/nio/Buffer.position)
monitor exit ()
, read(com/anarsoft/agent/regression    /MultiThreadedOneInstanceTemplate.threadCount)
volatile write(unknwon)

实际上,您也可以在源代码中看到这一点:

Actually you see this also in the source code:

public void  write(String str, int off, int len) throws IOException {
 synchronized (lock) {
     char cbuf[];
        if (len <= writeBufferSize) {
            if (writeBuffer == null) {
                writeBuffer = new char[writeBufferSize];
            }
            cbuf = writeBuffer;
        } else {    // Don't permanently allocate very large buffers.
            cbuf = new char[len];
        }
        str.getChars(off, (off + len), cbuf, 0);
        write(cbuf, 0, len);
    }
    }

这篇关于使用bufferwriter由文件写入器线程初始化的文件写入是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 17:16