class ABC{
private static Random random = new Random();
private static AtomicLong uniqueLongId = new AtomicLong(System.currentTimeMillis());
public static long getUniqueLongId(){
long id = uniqueLongId.incrementAndGet();
long uniqueID = Math.abs(random.nextLong()) + id;
return uniqueID;
//the above code we can write in one line
//return Math.abs(random.nextLong())+uniqueLongId.incrementAndGet();
}
}
上記のメソッドgetUniqueLongId()は、マルチスレッド環境で一意のIDを提供しますか?ここでの私の懸念は次のとおりです。uniqueLongIdがアトミックであり、incrementAndGet()の呼び出しがスレッドセーフな呼び出しであると想定していますが、コードの他の部分は同期されていません。これは、メソッドgetUniqueLongId()自体がスレッドセーフではないことを意味するのではないでしょうか。したがって、必ずしも一意のIDを返さない可能性がありますか?
説明してください..