Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does mkdir() sometimes not work?

I wrote this program to test a scenario in which mkdir() fails. Why is it failing?

Sometimes it works fine, and sometimes I get:

Cannot create DIR :: myDir4 Cannot create DIR :: myDir4

And in the last I found every directory is created...

On every test I delete all created directoryies.

I tried this because in my project there are 100 of threads trying to test and create directories like this... and also failing the same way...

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();
        }
    }
}
like image 964
Upendra Avatar asked Dec 20 '22 15:12

Upendra


1 Answers

You have a race condition.

Each thread tries to check each directory and create it if it doesn't exist yet. What is happening is something like this:

  • thread A tests myDir4 and finds it doesn't exist
  • thread B tests myDir4 and finds it doesn't exist
  • thread A creates myDir4 ... success!
  • thread B creates myDir4 ... failure! It already exists.

This could happen with any of the directories ... or not at all ... depending on exactly how the OS schedules the Java threads, etcetera.


Your code is attempting to synchronize on this, but the attempt is ineffective. The this will be the instance of CreteDir that the current thread is using ... but each thread will have a different instance so there is effectively no inter-thread synchronization. To synchronize effectively, all of the threads would need to synchronize on the same object ... but that would then render your multi-threading ineffective because the granularity is wrong.

In fact, your whole multi-threading strategy needs to be rethought. And the fact that this is not "the real code" means that we can't really advise you on how to do that.

Reading between the lines of your last comment, I think you have three possible strategies:

  • Just use a "global" lock to synchronize creation of the directories when they don't exist. Something like this:

    // 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!");
                }
            }
        }
    }
    
  • Create an in-memory data structure with one (lock) object per directory. Populating that data structure needs to be done carefully to avoid a race condition where two simultaneous client requests end up creating two lock objects for a single directory.

    (If you adjust this scheme a little, you can probably also use the existence of a lock objects to avoid repeatedly checking that the directory exists. The check involves a syscall and is non-trivial in terms of processor overheads.)

  • Just ignore the cases where File.mkdir() returns false. (Maybe do another File.exists() or File.isDirectory() test ... just in case.)

like image 106
Stephen C Avatar answered Dec 24 '22 01:12

Stephen C