Search in sources :

Example 6 with Guard

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;
    }
}
Also used : ArrayList(java.util.ArrayList) Guard(jetbrains.exodus.core.execution.locks.Guard)

Example 7 with Guard

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);
    }
}
Also used : Guard(jetbrains.exodus.core.execution.locks.Guard)

Example 8 with Guard

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);
}
Also used : Guard(jetbrains.exodus.core.execution.locks.Guard)

Example 9 with Guard

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntHashSet(jetbrains.exodus.core.dataStructures.hash.IntHashSet) Guard(jetbrains.exodus.core.execution.locks.Guard) Test(org.junit.Test)

Aggregations

Guard (jetbrains.exodus.core.execution.locks.Guard)9 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IntHashSet (jetbrains.exodus.core.dataStructures.hash.IntHashSet)1 Test (org.junit.Test)1