gpt4 book ai didi

java - 同时创建新文件

转载 作者:可可西里 更新时间:2023-11-01 13:31:45 26 4
gpt4 key购买 nike

<分区>

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

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

有什么想法吗?我已经在装有 java 1.7.0_45 和 1.8.0_31 的 Windows 8 机器上进行了测试,结果相同。

不确定问题是否与 this question 中的问题相同,但可以。在我看来,在同一进程中使用多个线程似乎是问题的一部分,但我不能确定这一点,但它的复制速度更快。

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