java 创建新文件_java – 同时创建新文件

在Java中,尝试在多线程环境中使用`synchronized`同步块创建唯一文件时,遇到`Access is denied`异常。代码示例显示了在创建和删除文件的过程中,线程间竞争可能导致的问题。异常主要出现在`File.createNewFile()`调用中,可能由于文件系统的并发访问限制。测试在Windows 8上使用Java 1.7.0_45和1.8.0_31时,问题重现率较高。
摘要由CSDN通过智能技术生成

参见英文答案 >

File.createNewFile() randomly fails                                    3个

要创建一个新的唯一文件名,我使用以下代码:

File file = new File(name);

synchronized (sync) {

int cnt = 0;

while (file.exists()) {

file = new File(name + " (" + (cnt++) + ")");

}

file.createNewFile();

}

接下来,我使用该文件并将其删除.

当我在多线程情况下执行此操作时,我有时会在file.createNewFile()上获得异常:

java.io.IOException: Access is denied

at java.io.WinNTFileSystem.createFileExclusively(Native Method)

at java.io.File.createNewFile(File.java:1012)

以下代码重现了该问题(大多数情况下):

final int runs = 1000;

final int threads = 5;

final String name = "c:\\temp\\files\\file";

final byte[] bytes = getSomeBytes();

final Object sync = new Object();

ExecutorService exec = Executors.newFixedThreadPool(threads);

for (int thread = 0; thread < threads; thread++) {

final String id = "Runnable " + thread;

exec.execute(new Runnable() {

public void run() {

for (int i = 0; i < runs; i++) {

try {

File file = new File(name);

synchronized (sync) {

int cnt = 0;

while (file.exists()) {

file = new File(name + " (" + (cnt++) + ")");

}

file.createNewFile();

}

Files.write(file.toPath(), bytes);

file.delete();

} catch (Exception ex) {

System.err.println(id + ": exception after " + i

+ " runs: " + ex.getMessage());

ex.printStackTrace();

return;

}

}

System.out.println(id + " finished fine");

}

});

}

exec.shutdown();

while (!exec.awaitTermination(1, TimeUnit.SECONDS));

方法getSomeBytes()只生成一定量的字节,实际内容并不重要:

byte[] getSomeBytes() throws UnsupportedEncodingException,

IOException {

byte[] alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ1234567890\r\n"

.getBytes("UTF-8");

try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

for (int i = 0; i < 100000; i++) {

baos.write(alphabet);

}

baos.flush();

return baos.toByteArray();

}

}

当我执行这段代码时,它有时会顺利进行,但大部分时间都会产生一些异常,例如下面的输出:

Runnable 1: exception after 235 runs: Access is denied

java.io.IOException: Access is denied

at java.io.WinNTFileSystem.createFileExclusively(Native Method)

at java.io.File.createNewFile(File.java:1012)

at test.CreateFilesTest$1.run(CreateFilesTest.java:36)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:745)

Runnable 4: exception after 316 runs: Access is denied

java.io.IOException: Access is denied

at java.io.WinNTFileSystem.createFileExclusively(Native Method)

at java.io.File.createNewFile(File.java:1012)

at test.CreateFilesTest$1.run(CreateFilesTest.java:36)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:745)

Runnable 2: exception after 327 runs: Access is denied

java.io.IOException: Access is denied

at java.io.WinNTFileSystem.createFileExclusively(Native Method)

at java.io.File.createNewFile(File.java:1012)

at test.CreateFilesTest$1.run(CreateFilesTest.java:36)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:745)

Runnable 3 finished fine

Runnable 0 finished fine

有任何想法吗?我已经在Windows 8机器上测试了java 1.7.0_45和1.8.0_31,结果相同.

不确定问题是否与this question中的相同,但可以.在我看来,在同一过程中使用多个线程似乎是问题的一部分,但我无法确定这一点,但它的再现速度更快.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值