Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:24:01+00:00 2026-06-12T13:24:01+00:00

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

  • 0

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();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:24:03+00:00Added an answer on June 12, 2026 at 1:24 pm

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a very simple program named test.py which looks like this: print 'hello
I wrote this short program which has a tiny GUI. Its supposed to allow
I wrote this program in Java public class Why { public static void test()
I wrote a test program that looked like this: #!/usr/bin/python def incrementc(): c =
I wrote a test program like this: #include <sys/socket.h> int main( void ) {
I wrote a simple test program in c++ but why does this crash on:
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
I'm trying to understand concurrency in Go. In particular, I wrote this thread-unsafe program:
I wrote this function to communicate with an external program. Such program takes input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.