mirror of https://github.com/apache/activemq.git
AMQ-7300 - Improve performance of LongSequenceGenerator
LongSequenceGenerator now uses an AtomicLongFieldUpdater instead of synchronized
This commit is contained in:
parent
dc35218a2d
commit
4db2656066
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue