我编写这个程序是为了测试 mkdir()
失败的场景。为什么会失败?
有时它工作正常,有时我得到:
Cannot create DIR :: myDir4 Cannot create DIR :: myDir4
最后我发现每个目录都已创建...
在每次测试中我都会删除所有创建的目录。
我试过这个,因为在我的项目中有 100 个线程试图测试和创建这样的目录......并且也以同样的方式失败......
public class DFS {
static long time1 = System.currentTimeMillis();
public static void main(String a[]) {
new Thread(new CreteDir()).start();
new Thread(new CreteDir()).start();
new Thread(new CreteDir()).start();
new Thread(new CreteDir()).start();
new Thread(new CreteDir()).start();
new Thread(new CreteDir()).start();
new Thread(new CreteDir()).start();
new Thread(new CreteDir()).start();
}
}
class CreteDir implements Runnable {
public void run() {
//Object obj = new Object();
synchronized (this) {
if(System.currentTimeMillis() - DFS.time1 > 10) {
try {
this.wait();
}
catch(InterruptedException ie) {
ie.printStackTrace();
}
}
File f1 = new File("myDir1");
File f2 = new File("myDir2");
File f3 = new File("myDir3");
File f4 = new File("myDir4");
File f5 = new File("myDir5");
if (!f1.exists()&&!f1.mkdir()) {
System.out.println("Cannot create DIR :: "+f1.getName());
}
if (!f2.exists()&&!f2.mkdir()) {
System.out.println("Cannot create DIR :: "+f2.getName());
}
if (!f3.exists()&&!f3.mkdir()) {
System.out.println("Cannot create DIR :: "+f3.getName());
}
if (!f4.exists()&&!f4.mkdir()) {
System.out.println("Cannot create DIR :: "+f4.getName());
}
if (!f5.exists()&&!f5.mkdir()) {
System.out.println("Cannot create DIR :: "+f5.getName());
}
this.notifyAll();
}
}
}
最佳答案
你有一个竞争条件。
每个线程都会尝试检查每个目录,如果目录不存在则创建它。发生的事情是这样的:
- 线程
A
测试myDir4
,发现不存在 - 线程
B
测试myDir4
,发现不存在 - 线程
A
创建myDir4
... 成功! - thread
B
创建myDir4
... 失败!它已经存在。
任何目录都可能发生这种情况……或者根本不会发生……具体取决于操作系统如何安排 Java 线程等。
您的代码正尝试在 this
上同步,但尝试无效。 this
将是当前线程正在使用的 CreteDir
的实例……但每个线程都有不同的实例,因此实际上没有线程间同步。为了有效地同步,所有线程都需要在同一个对象上同步……但这会使您的多线程无效,因为粒度是错误的。
事实上,您的整个多线程策略需要重新考虑。事实上,这不是“真正的代码”,这意味着我们无法真正建议您如何去做。
阅读您上一条评论的字里行间,我认为您有三种可能的策略:
当目录不存在时,只需使用“全局”锁来同步目录的创建。像这样:
// Test first without locking to reduce the concurrency bottleneck if (!dir.exists()) { synchronize (globalDirLock) { // Repeat the test while holding the lock if (!dir.exists()) { if (!dir.mkdir()) { System.out.println("OOOPS!"); } } } }
创建一个内存数据结构,每个目录有一个(锁)对象。填充该数据结构需要小心进行,以避免出现竞争条件,即两个同时发生的客户端请求最终会为单个目录创建两个锁定对象。
(如果你稍微调整一下这个方案,你可能还可以使用锁对象的存在来避免重复检查目录是否存在。检查涉及系统调用并且在处理器开销方面是不平凡的。)
忽略
File.mkdir()
返回false
的情况。 (也许再做一次File.exists()
或File.isDirectory()
测试……以防万一。)
关于java - 为什么 mkdir() 有时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12768057/