public class DataEvent {
private static final AtomicInteger lastRevision = new AtomicInteger();
private final int revision;
private final long threadId;
private final long timestamp;
private DataEvent(int revision) {
this.revision = revision;
this.threadId = Thread.currentThread().getId();
this.timestamp = System.nanoTime();
}
public static DataEvent newInstance() {
return new DataEvent(lastRevision.incrementAndGet());
}
}
My questions are following:
- is it absolutely correct to say that all objects will be constructed consistently one by one? I mean that every new object is constructed later then previous one. In other words each new object has
timestamp
that is bigger then previous one. - how
final
keyword affects this behavior? As I understand if all object fields arefinal
then it makes constructor atomic in some way. Reference is not published until allfinal
fields are initialized. - what is best practice to construct such objects? Is is enough to make
lastRevision
atomic ornewInstance
should be declared assynchronized
?