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;
|
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 {
|
public class LongSequenceGenerator {
|
||||||
|
|
||||||
private long lastSequenceId;
|
private static final AtomicLongFieldUpdater<LongSequenceGenerator> SEQUENCE_UPDATER =
|
||||||
|
AtomicLongFieldUpdater.newUpdater(LongSequenceGenerator.class, "lastSequenceId");
|
||||||
|
|
||||||
public synchronized long getNextSequenceId() {
|
private volatile long lastSequenceId;
|
||||||
return ++lastSequenceId;
|
|
||||||
|
public long getNextSequenceId() {
|
||||||
|
return SEQUENCE_UPDATER.incrementAndGet(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized long getLastSequenceId() {
|
public long getLastSequenceId() {
|
||||||
return lastSequenceId;
|
return SEQUENCE_UPDATER.get(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void setLastSequenceId(long l) {
|
public void setLastSequenceId(long l) {
|
||||||
lastSequenceId = l;
|
SEQUENCE_UPDATER.set(this, l);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue