Which class implements IdGenerator in a thread-safe manner, so that no threads can get a duplicate id value current access?

Given the interface:

Public interface Idgenerator {

int getNextId();

}

Which class implements IdGenerator in a thread-safe manner, so that no threads can get a duplicate id value current access?

Given the interface:

Public interface Idgenerator {

int getNextId();

}

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 AtomicInteger id = new AtomicInteger (0);
return id.incrementAndget();
}
}

B.
Public class Generator Implements idGenerator {
private int id = 0;
return ++id;
}
}

C.
Public class Generator Implements IdGenerator {
private volatile int Id = 0;
return + + Id;
}

D.
Public class Generator Implements IdGenerator {
private int id = 0;
public int getNextId() {
synchronized (new Generator()) {
return + + id;
}
}
}

E.
Public class Generator Implements IdGenerator {
private int id = 0;
public int getnextId() {
synchronized (id) {
return + + id;
}
}
}

Explanation:
Code that is safe to call by multiple threads simultanously is called thread safe. If a piece of code is thread safe, then it contains no race conditions. Race condition only occur when multiplethreads update shared resources. Therefore it is important to know what resources Java threads share when executing.

In Java you can mark a method or a block of code as synchronized. Synchronized blocks can be used to avoid race conditions.



Leave a Reply 9

Your email address will not be published. Required fields are marked *


Julia

Julia

My opinion is E) because the “new Generator” is bullshit….
BUT E) is only 95% OK, because the “int” should be an “Integer”. Synchronized(Object o) needs an object

Francesco

Francesco

For this example, synchronization is an acceptable solution. But for a more complicated class, we might want to avoid the liveness impact of unnecessary synchronization. Replacing the int field with an AtomicInteger allows us to prevent thread interference without resorting to synchronization.

Answer A

http://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html

Serg

Serg

Answer A.

Atomic Access http://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html

Atomic Variables http://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html

class AtomicCounter {
private AtomicInteger c = new AtomicInteger(0);

public void increment() {
c.incrementAndGet();
}

public void decrement() {
c.decrementAndGet();
}

public int value() {
return c.get();
}

}

Dylan

Dylan

The only two answers that implement a getNextId() method are D & E, but seeing as you can’t use a primitive type for synchronization purposes, the nearest to a correct answer would seem to be D. That is unless A, B and C are misprints – they have return statements outside of any method! A, would be correct if the return statement was in a getNextId() method.