I'm aware that including the word synchronized to the getNext() method signature would make the method completely thread safe. But what if in the body of the method I surround only the portion where the actual incrementing is being done with a synchronized block? Will this still be a completely thread safe incrementer? What's the difference?
public class NextNumberGenerator {
private int number = 0;
public int getNext() {
synchronized (this) {
number = number + 1;
}
return number;
}
}