Java is unstable and / or synchronized

I have a static method that should generate a unique identifier based on the current timestamp, as shown in the codes below. To make sure that the newly generated identifier does not match the previously generated identifier (due to a very fast computer, so the millisecond does not change), I set a loop to compare the newly generated identifier with the previously generated identifier. If they match, it will generate another identifier.

public class Util {

    protected static String uniqueID;

    public static String generateUniqueID() {
        SimpleDateFormat timstampFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        do {
            String timestamp = timstampFormat.format(new Date());
            if (!timestamp.equals(uniqueID)) {
                uniqueID = timestamp;
                return uniqueID;
            }
        } while (true);
    }

}

I want the codes above to work when a method is called by multiple threads.

If I just put the volatile keyword in the uniqueID variable, would that be good enough? Should I have a synchronized block?

, volatile?

.

:

, volatile?

public class Util {

    private static volatile String uniqueID;

    public static synchronized String generateUniqueID() {
        uniqueID = UUID.randomUUID().toString();
        return uniqueID;
    }

}
+4
4

.

UID, , UID , , , . , timestamp- . System.nanoTime() , .

while , UID . , , CPU. , , .

volatile . , synchronized, . , 1000 UID . , , . .

:
. , , . concurrency. UID .

:
-, ? UID-, Oracle. UUID, , , , UID. UID.

+7

, , , , , , . , . - , a >= , .

private static final AtomicLong time = new AtomicLong(0);
public static long uniqueTimedId() {
    while(true) {
         long now = System.currentTimeMillis();
         long value = time.get();
         long next = now > value ? now : value + 1;
         if (time.compareAndSwap(value, next))
            return next;
    }
}

, . , . , . , 1 .

private static final AtomicLong time = new AtomicLong(0);
public static long uniqueTimedId() {
    while(true) {
         long now = System.currentTimeMillis() * 1000;
         long value = time.get();
         long next = now > value ? now : value + 1;
         if (time.compareAndSwap(value, next))
            return next;
    }
}

, 1000x . , 1000 , String, 1000, , x % 1000 .

10. 10 .

/ , , .

/, , .

. , volatile. .

, volatile?

synchronized , , . volatile .

private static String uniqueID;

public static synchronized String generateUniqueID() {
    uniqueID = UUID.randomUUID().toString();
    // without synchronized, one thread could read what another thread wrote
    return uniqueID;
}

, , .

// nothing is shared, so no thread safety issues.
public static String generateUniqueID() {
    return UUID.randomUUID().toString();
}
+2

, , , - . Volatile , , .

, ,

+1

uniqueID volatile, , , UUID ( ) ( , , MAC-), :

public static String generateUniqueID() {
    return UUID.randomUUID().toString();
}

public static UUID randomUUID()

factory 4 ( ) UUID. UUID .

+1
source

Source: https://habr.com/ru/post/1659388/


All Articles