AMQ-7300 - Improve performance of LongSequenceGenerator

LongSequenceGenerator now uses an AtomicLongFieldUpdater instead of
synchronized
This commit is contained in:
Christopher L. Shannon (cshannon) 2019-09-06 11:55:02 -04:00
parent dc35218a2d
commit 4db2656066
1 changed files with 17 additions and 8 deletions

View File

@ -16,20 +16,29 @@
*/
package org.apache.activemq.util;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
/**
* Updated LongSequenceGenerator that uses an AtomicLongFieldUpdater for performance instead of
* synchronized methods
*/
public class LongSequenceGenerator {
private long lastSequenceId;
private static final AtomicLongFieldUpdater<LongSequenceGenerator> SEQUENCE_UPDATER =
AtomicLongFieldUpdater.newUpdater(LongSequenceGenerator.class, "lastSequenceId");
public synchronized long getNextSequenceId() {
return ++lastSequenceId;
private volatile long lastSequenceId;
public long getNextSequenceId() {
return SEQUENCE_UPDATER.incrementAndGet(this);
}
public synchronized long getLastSequenceId() {
return lastSequenceId;
public long getLastSequenceId() {
return SEQUENCE_UPDATER.get(this);
}
public synchronized void setLastSequenceId(long l) {
lastSequenceId = l;
public void setLastSequenceId(long l) {
SEQUENCE_UPDATER.set(this, l);
}
}