Z
Zhao
Here is the issue I want to discuss
"
....
private int foo;
public synchronized int getFoo() { return foo; }
public synchronized void setFoo(int f) { foo = f; }
If a caller wants to increment the foo property, the following code to
do so is not thread-safe:
...
setFoo(getFoo() + 1);
If two threads attempt to increment foo at the same time, the result
might be that the value of foo gets increased by one or by two,
depending on timing.
....
"
I couldn't figure out why it not thread-safe...
Any advice will be appreciated.
thanks
"
....
private int foo;
public synchronized int getFoo() { return foo; }
public synchronized void setFoo(int f) { foo = f; }
If a caller wants to increment the foo property, the following code to
do so is not thread-safe:
...
setFoo(getFoo() + 1);
If two threads attempt to increment foo at the same time, the result
might be that the value of foo gets increased by one or by two,
depending on timing.
....
"
I couldn't figure out why it not thread-safe...
Any advice will be appreciated.
thanks