Skip to content

Commit

Permalink
Synchronized updates on volatile members
Browse files Browse the repository at this point in the history
Increments (++foo) and compound assignments (foo += bar) are
non-atomic which is why updates need to be synchronized.
  • Loading branch information
markusfisch committed Jun 24, 2017
1 parent 9668d20 commit 136b61e
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -866,11 +866,16 @@ private byte[] saveThumbnail() {
}

private void updateFps(long now) {
sum += Math.min(NS_PER_SECOND / (now - lastRender), 60f);
long delta = now - lastRender;

if (++samples > 0xffff) {
sum = sum / samples;
samples = 1;
// because sum and samples are volatile
synchronized (this) {
sum += Math.min(NS_PER_SECOND / delta, 60f);

if (++samples > 0xffff) {
sum = sum / samples;
samples = 1;
}
}

if (now > nextFpsUpdate) {
Expand Down

0 comments on commit 136b61e

Please sign in to comment.