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 that is bigger then previous one.timestamp
how keyword affects this behavior? As I understand if all object fields are then it makes constructor atomic in some way. Reference is not published until all fields are initialized.finalfinalfinal
what is best practice to construct such objects? Is is enough to make atomic or should be declared as ?lastRevisionnewInstancesynchronized
元芳怎么了
冉冉说
心有法竹
随时随地看视频慕课网APP
相关分类