mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
#2098 - Refactor simplify SequenceIdGenerator.loadInBackground() to use AtomicBoolean
This commit is contained in:
1 parent
1eb5cf1d22
commit
f473e40bfc
1 file changed
+11
-21
@@ -16,6 +16,7 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.NavigableSet;
|
import java.util.NavigableSet;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,7 +43,7 @@ public abstract class SequenceIdGenerator implements PlatformIdGenerator {
|
|||||||
|
|
||||||
protected final int allocationSize;
|
protected final int allocationSize;
|
||||||
|
|
||||||
protected boolean currentlyBackgroundLoading;
|
protected AtomicBoolean currentlyBackgroundLoading = new AtomicBoolean(false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct given a dataSource and sql to return the next sequence value.
|
* Construct given a dataSource and sql to return the next sequence value.
|
||||||
@@ -128,27 +129,16 @@ public abstract class SequenceIdGenerator implements PlatformIdGenerator {
|
|||||||
* Load another batch of Id's using a background thread.
|
* Load another batch of Id's using a background thread.
|
||||||
*/
|
*/
|
||||||
protected void loadInBackground(final int requestSize) {
|
protected void loadInBackground(final int requestSize) {
|
||||||
// single threaded processing...
|
if (currentlyBackgroundLoading.get()) {
|
||||||
loadLock.lock();
|
// skip as already background loading
|
||||||
try {
|
logger.debug("... skip background sequence load (another load in progress)");
|
||||||
if (currentlyBackgroundLoading) {
|
return;
|
||||||
// skip as already background loading
|
|
||||||
logger.debug("... skip background sequence load (another load in progress)");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
currentlyBackgroundLoading = true;
|
|
||||||
backgroundExecutor.execute(() -> {
|
|
||||||
loadMore(requestSize);
|
|
||||||
loadLock.lock();
|
|
||||||
try {
|
|
||||||
currentlyBackgroundLoading = false;
|
|
||||||
} finally {
|
|
||||||
loadLock.unlock();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
loadLock.lock();
|
|
||||||
}
|
}
|
||||||
|
currentlyBackgroundLoading.set(true);
|
||||||
|
backgroundExecutor.execute(() -> {
|
||||||
|
loadMore(requestSize);
|
||||||
|
currentlyBackgroundLoading.set(false);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user