use of jetbrains.exodus.core.execution.locks.Guard in project xodus by JetBrains.
the class JobProcessorQueueAdapter method doTimedJobs.
protected void doTimedJobs() {
final Collection<Job> outdatedJobs = new ArrayList<>();
final long currentTimePriority = Long.MAX_VALUE - System.currentTimeMillis();
int count;
try (Guard ignored = timeQueue.lock()) {
Pair<Long, Job> pair = timeQueue.peekPair();
while (pair != null && pair.getFirst() >= currentTimePriority) {
outdatedJobs.add(timeQueue.pop());
pair = timeQueue.peekPair();
}
count = outdatedJobs.size();
}
// outdatedJobsCount is updated in single thread, so we won't bother with synchronization on its update
outdatedJobsCount = count;
for (final Job job : outdatedJobs) {
executeImmediateJobsIfAny();
if (isFinished()) {
break;
}
doExecuteJob(job);
outdatedJobsCount = --count;
}
}
use of jetbrains.exodus.core.execution.locks.Guard in project xodus by JetBrains.
the class JobProcessorQueueAdapter method doJobs.
protected void doJobs() {
final boolean jobsQueued;
try {
jobsQueued = waitForJobs();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
try {
if (!isFinished()) {
if (jobsQueued) {
final Job job;
try (Guard ignored = queue.lock()) {
job = queue.pop();
}
doExecuteJob(job);
} else {
doTimedJobs();
}
}
} catch (Throwable t) {
handleThrowable(null, getExceptionHandler(), t);
}
}
use of jetbrains.exodus.core.execution.locks.Guard in project xodus by JetBrains.
the class JobProcessorQueueAdapter method waitForJobs.
// returns true if a job was queued within a timeout
protected boolean waitForJobs() throws InterruptedException {
final Pair<Long, Job> peekPair;
try (Guard ignored = timeQueue.lock()) {
peekPair = timeQueue.peekPair();
}
if (peekPair == null) {
awake.acquire();
return true;
}
final long timeout = Long.MAX_VALUE - peekPair.getFirst() - System.currentTimeMillis();
if (timeout < 0) {
return false;
}
return awake.tryAcquire(timeout, TimeUnit.MILLISECONDS);
}
use of jetbrains.exodus.core.execution.locks.Guard in project xodus by JetBrains.
the class PriorityQueueTest method concurrentBenchmark.
@SuppressWarnings("ObjectAllocationInLoop")
@Test
public void concurrentBenchmark() {
final AtomicInteger counter = new AtomicInteger();
final PriorityQueue<Priority, TestObject> queue = createQueue();
final Runnable threadFunction = () -> {
try {
// noinspection InfiniteLoopStatement
while (true) {
try (Guard ignored = queue.lock()) {
final TestObject value = new TestObject(counter);
final Priority p;
switch(value.number % 5) {
case 0:
p = Priority.lowest;
break;
case 1:
p = Priority.below_normal;
break;
case 2:
p = Priority.normal;
break;
case 3:
p = Priority.above_normal;
break;
default:
p = Priority.highest;
break;
}
queue.push(p, value);
}
}
} catch (RuntimeException e) {
// ignore
}
};
TestUtil.time("concurrentBenchmark", () -> {
final int numberOfThreads = 4;
final Thread[] threads = new Thread[numberOfThreads];
for (int i = 0; i < numberOfThreads; ++i) {
threads[i] = new Thread(threadFunction);
}
for (int i = 0; i < numberOfThreads; ++i) {
threads[i].start();
}
for (int i = 0; i < numberOfThreads; ++i) {
try {
threads[i].join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
// ignore
}
}
});
final IntHashSet numbers = new IntHashSet();
TestObject to;
while ((to = queue.pop()) != null) {
numbers.add(to.number);
}
assertEquals(TestObject.MAX_TEST_OBJECTS, numbers.size());
}
Aggregations