A Discussion on the thread security of the file. mkdirs method in Java

Source: Internet
Author: User
A Discussion on the thread security of the file. mkdirs method in Java

Article category: Java programming

Mkdirs is a method used in Java to create a multi-level directory at a time, in the Java. Io. File class.

In programming, I encountered the following code:
If (! DeST. exists ()){
DeST. mkdirs ();
}
If (! DeST. isdirectory ()){
Throw new ioexception (DEST. getname () + "must be a directory! ");
}

The method of the code segment is called by multiple threads. In some running times, the directory created by the DeST. mkdirs method fails, causing the Exception Code to be thrown in the following if statement to be executed.

The Java. Io. file. mkdirs code in JDK 1.5 update 21 is as follows:
Public Boolean mkdirs (){
If (exists ()){
Return false;
}
If (mkdir ()){
Return true;
}
File canonfile = NULL;
Try {
Canonfile = getcanonicalfile ();
} Catch (ioexception e ){
Return false;
}
String parent = canonfile. getparent ();
Return (parent! = NULL )&&
(New file (parent, FS. prefixlength (parent). mkdirs ()&&
Canonfile. mkdir ());
}

Check the mkdirs source code and find that there is a problem in this possible situation: both threads call the code at the beginning, and the directory to be created in the first thread is
"Dirparent/Dira". The directory to be created by the second thread is "dirparent/dirb" (The dirparent directory does not exist before execution ). Both threads
In the mkdirs method, neither thread can directly create a directory and execute the line of the "return" statement. Next, the first thread is successfully executed and created.
"Dirparent/Dira" directory, when the second thread advances, the recursive call to the mkdirs method returns false because the "dirparent" directory already exists,
By using the short-circuit mechanism of the & operator, the "canonfile. mkdir ()" statement is not executed, that is, the "dirb" directory is not created and
Mkdirs also returns false. Therefore, in the second thread, the statement that throws an exception is executed.

In this case, the mkdirs method is not thread-safe. For the actual situation of this program, I changed the code above to the following (fileutils is the class where the code is located, and the method where the code is located is static ):
If (! DeST. exists ()){
Synchronized (fileutils. Class ){
DeST. mkdirs ();
}
}
If (! DeST. isdirectory ()){
Throw new ioexception (DEST. getname () + "must be a directory! ");
}
In this way, the problem is solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.