• Post Reply Bookmark Topic Watch Topic
  • New Topic

About mkdirs()  RSS feed

 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I found this article on File's mkdirs() method.

I just want to know weather i should follow this approach.

Please let me know.

Thanks.

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The article title is "Is File.mkdirs() Thread Safe". So it really depends on what you are using mkdir() for?

Also if you mkdir on Unix/Linux and on Windows the behavior may be different because on Windows at least the mkdir() will practically always succeed unless you explicitly set the folder permission. But for unix/linux, every folder permission may be different and I don't recall what the default is.
 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you K. Tsang for quick and sincere response.

Well ultimately my program will run on unix.

I just want to know weather i should use mkdirs() in this manner



 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Let see what does mkdirs do? It makes or create the directory. So since you running on Unix, if the if statement fails... it makes the directory. But then think about it if the first try fails, how possibly the second try will succeed?

I suggest using the file.exist() instead. Something like:


 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you K. Tsang.
Somehow i agree with you. but did you read this paragraph of that article which i given earlier--



File.mkdirs() is not thread safe.

Actually the concurrency problem is not only between threads in a same process but across processes also. The problem is the way in which the mkdirs() function is implemented. It first tries to check whether each of the parent directories exist, and if a parent directory does not exist, then it is created using the mkdir() function.

suppose two threads try to create the following two directories at the same time

Thread1: "root_dir/CoreJava/Threads"

Thread2: "root_dir/CoreJava/Swings"

Now, Thread1 finds that the directory "root_dir/CoreJava" does not exist and tries to create it using the mkdir() function, now suppose by chance Thread2 also finds that the same directory does not exist and tries to create it using the mkdir() function, one of these threads will first create the directory "root_dir/CoreJava" and for the other thread, the mkdir() function will return false because, the directory "root_dir/CoreJava" now already exists, this causes mkdirs() function to think that the creation of a parent directory has failed, and it does not try to create the rest of the directory structure


Please go through it. And the tell me the approach to go throgh.

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The claim made in the article is easily refuted by looking at the JDK source. In Java 6, anyway (I didn't look at any earlier version) mkdirs() clearly does not have this problem. It a mkdir() call returns false, the code explicitly checks whether the new directory actually exists. Perhaps this was a bug at one time, but no longer.
 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ernest Friedman-Hill for giving me clarification.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!