harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From regi...@apache.org
Subject svn commit: r960424 - /harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/File.java
Date Mon, 05 Jul 2010 03:05:17 GMT
Author: regisxu
Date: Mon Jul  5 03:05:16 2010
New Revision: 960424

URL: http://svn.apache.org/viewvc?rev=960424&view=rev
Log:
make File.createTempFile thread-safe to avoid to return the same file multiple times

File.counter could be accessed by multiple threads, so use AtomicInteger to make
sure each thread using different int value to create temp file.

Modified:
    harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/File.java

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/File.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/File.java?rev=960424&r1=960423&r2=960424&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/File.java (original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/File.java Mon
Jul  5 03:05:16 2010
@@ -24,6 +24,7 @@ import java.security.AccessController;
 import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.harmony.luni.internal.io.FileCanonPathCache;
 import org.apache.harmony.luni.util.DeleteOnExit;
@@ -79,7 +80,7 @@ public class File implements Serializabl
     public static final String pathSeparator;
 
     /* Temp file counter */
-    private static int counter;
+    private static AtomicInteger counter = new AtomicInteger(0);
 
     private static boolean caseSensitive;
 
@@ -1300,13 +1301,13 @@ public class File implements Serializabl
     }
 
     private static File genTempFile(String prefix, String suffix, File directory) {
-        if (counter == 0) {
+        if (counter.get() == 0) {
             int newInt = new SecureRandom().nextInt();
-            counter = ((newInt / 65535) & 0xFFFF) + 0x2710;
+            counter.compareAndSet(0, ((newInt / 65535) & 0xFFFF) + 0x2710);
         }
         StringBuilder newName = new StringBuilder();
         newName.append(prefix);
-        newName.append(counter++);
+        newName.append(counter.getAndIncrement());
         newName.append(suffix);
         return new File(directory, newName.toString());
     }



Mime
View raw message