tags:

views:

422

answers:

5
String s = UUID.randomUUID().toString(); 
return s.substring(0,8) + s.substring(9,13) + s.substring(14,18) +
       s.substring(19,23) + s.substring(24);

I use JDK1.5's UUID, but it uses too much time when I connect/disconnect from the net.
I think the UUID may want to access some net.
Can anybody help me?

+2  A: 

UUID generation is done locally and doesn't require any alive network connection.

sharptooth
A: 

The javadoc for UUID http://java.sun.com/j2se/1.5.0/docs/api/java/util/UUID.html has some good information on how the UUID is generated. It uses the time and clock frequency to generate the UUID. Like sharptooth says, no network interface is required. Is there possibly some other concurrent process running that could possibly be causing this problem?

Actually, a network interface would be required to populate type 1 UUIDs, but those are not being used here.
Michael Borgwardt
+2  A: 

Quoting the API odc:

public static UUID randomUUID()

Static factory to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.

Your delay is probably being caused by the intialization of the cryptographically strong RNG - those take some time, and might even depend on the presence of a network connection as a source of entropy. However, this should happen only once during the runtime of the JVM. I don't see a way around this problem, though.

Michael Borgwardt
A: 

What's the purpose of those s.substring calls? It looks like you're returning the original string.

lumpynose
He's cutting the '-' out of the UUID
Aaron Digulla
A: 

If you're appending 5 Strings together, over a large set of data, that could be the issue. Try to use StringBuffer. It's amazing the difference that can make when concatenating more than 1-2 Strings together, especially for larger datasets

Stephane Grenier