Given the interface: Which class implements IdGenerator in a thread-safe manner, so that
no threads can get a duplicate id value current access?
A.
Public class Generator Implements IdGenerator { private int id = 0; public int getnextId() {
synchronized (id) { return ++id; }}}
B.
Public class Generator Implements IdGenerator { private int id = 0; public int getNextId() {
synchronized (new Generator()) { return ++id; }}}
C.
Public class Generator Implements IdGenerator { private volatile int Id = 0; return ++Id; }
D.
Public class generator Implements IdGenerator { Private AtomicInteger id = new
AtomicInteger (0); return id.incrementAndget(); }}
E.
Public class Generator Implements idGenerator { private int id = 0; return ++id; }}
In the C D E answers, the “return” statements should be read as if they were wrapped in the public int getNextId() method, as in A and B.
And the marked answer B is wrong, D is correct