gpt4 book ai didi

java - 使用文件写入器线程初始化的缓冲写入器写入文件是否安全?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:08:49 24 4
gpt4 key购买 nike

是否线程安全?如果是,为什么;

这是我的代码的一部分

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

最佳答案

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

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生成以下跟踪:

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)

实际上你在源代码中也看到了这一点:

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

关于java - 使用文件写入器线程初始化的缓冲写入器写入文件是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30080560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com