UUIDGenerator generates same ID multiple times when invoked from multiple 
threads
---------------------------------------------------------------------------------

                 Key: WSCOMMONS-201
                 URL: https://issues.apache.org/jira/browse/WSCOMMONS-201
             Project: WS-Commons
          Issue Type: Bug
          Components: AXIOM
         Environment: Ubuntu 6.06, JDK 1.5
            Reporter: Chathura Ekanayake


org.apache.axiom.om.util.UUIDGenerator generates same UUID more than once, when 
the UUIDGenerator.getUUID() is invoked from multiple threads concurrently. I 
have tested this with 100 threads, each invoking the getUUID() 1000 times. 
UUIDGenerator generates about 1500 to 3000 same IDs for 100000 invocations.

I am listing the code I have used to test this below:

package uuidtest;

import org.apache.axiom.om.util.UUIDGenerator;

import java.util.*;

public class UUIDGenTest {

   private static final int THREADS = 100;
   private static final int ITERATIONS = 1000;

   Map generatedIds = Collections.synchronizedMap(new HashMap());
    int errorCount = 0;
   Object lock = new Object();

   public static void main(String[] args) {
       new UUIDGenTest().test();
   }

   public void test() {

       Thread[] threads = new Thread[THREADS];
       for (int i = 0; i < THREADS; i++) {
           threads[i] = new Thread(new Generate());
           threads[i].start();
       }

       for (int i = 0; i < THREADS; i++) {
           try {
               threads[i].join();
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
       
System.out.println("=======================================================");
       System.out.println("  Error count : " + errorCount + " for " + THREADS * 
ITERATIONS);
       
System.out.println("=======================================================");
   }

   class Generate implements Runnable {
       public void run() {
           for (int i=0; i<ITERATIONS; i++) {
               String id = UUIDGenerator.getUUID();
               if (generatedIds.containsKey(id)) {
                   System.out.println("ERROR - Same UUID has been generated 
before. UUID: " + id);
                   synchronized(lock) {
                       errorCount++;
                   }
               }
               generatedIds.put(id, Thread.currentThread().getName());
           }
       }
   }
}

If you compile and run this with AXIOM jars in the classpath, you could observe 
an error count about 1500 to 3000.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to