| Is File.mkdirs() Thread Safe? |
| Did You Know? | ||||||
| Written by rajesh | ||||||
| Tuesday, 13 May 2008 02:48 | ||||||
|
The File class in java.io package provides an abstract representation of file and directory pathnames. This class provides many useful functions for operating on files. But no where in the Api documentation of the java.io.File class is mentioned about the thread safety of the methods in this class. In this article, we shall see whether the mkdirs() method in File is thread safe. Problems came when I was using the mkdirs() function to create directory structures. This function creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. File temp = new File("C:/parent/child/grandchild"); temp.mkdirs(); It will first create the non existent parent directories "C:/parent", then "C:/parent/child", and then finally create our directory "C:/parent/child/grandchild". The functionality seems pretty clear and safe. But, in my application, the mkdirs() function failed to create directories in some rare cases. My application is a Content Management system just like Joomla, In my application all data is stored as files instead of databases. Do if there are sections and categories, they will be stored in the disk as directory structures. "root_dir/SectionName/CategoryName" My application also provides an option to migrate from joomla. Joomla stores the data in databases, and to migrate from Jooma to my application, my application has to read the section, category and articles information from Joomla database and create corresponding directory structures. To improve the speed of this migration process I had used multiple threads. Though, no problems were initially visible, one of my testers reported that even though the migration was reported to be successful, some articles were not copied. This made me wonder for hours, what might have gone wrong. When I analyzed, I found that the directories for some categories were not created at all. This made me feel that there might be some problem in mkdirs() so I searched on the internet and I found this. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4742723 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. This is why, the directories for some categories were not created at all.
The current workaround to this problem is to call the mkdirs() function twice as below if(!file.mkdirs("...") { file.mkdirs("..."); } Hope, you would have enjoyed this article. I will keep posting more interestin articles. Keep visiting.
Only registered users can write comments!
Powered by !JoomlaComment 3.22
3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
||||||
| Last Updated ( Tuesday, 13 May 2008 03:30 ) | ||||||








