use of java.util.concurrent.locks.ReentrantLock in project lucene-solr by apache.
the class BaseLockFactoryTestCase method testObtainConcurrently.
public void testObtainConcurrently() throws InterruptedException, IOException {
Path tempPath = createTempDir();
final Directory directory = getDirectory(tempPath);
final AtomicBoolean running = new AtomicBoolean(true);
final AtomicInteger atomicCounter = new AtomicInteger(0);
final ReentrantLock assertingLock = new ReentrantLock();
int numThreads = 2 + random().nextInt(10);
final int runs = atLeast(10000);
CyclicBarrier barrier = new CyclicBarrier(numThreads);
Thread[] threads = new Thread[numThreads];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread() {
@Override
public void run() {
try {
barrier.await();
} catch (Exception e) {
throw new RuntimeException(e);
}
while (running.get()) {
try (Lock lock = directory.obtainLock("foo.lock")) {
assertFalse(assertingLock.isLocked());
if (assertingLock.tryLock()) {
assertingLock.unlock();
} else {
fail();
}
// stupid compiler
assert lock != null;
} catch (IOException ex) {
//
}
if (atomicCounter.incrementAndGet() > runs) {
running.set(false);
}
}
}
};
threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
directory.close();
}
use of java.util.concurrent.locks.ReentrantLock in project geode by apache.
the class Producer method oneRun.
public static void oneRun() {
DeathRow = new ReentrantLock();
End = new ReentrantLock();
EndCondition = End.newCondition();
nDead = nUp = 0;
long cyBase = System.currentTimeMillis();
DeathRow.lock();
try {
for (int i = 1; i <= nThreads; i++) {
new Producer("Producer" + i).start();
}
try {
End.lock();
try {
while (nDead != nThreads) EndCondition.await();
} finally {
End.unlock();
}
} catch (Exception ex) {
System.out.println("Exception in End: " + ex);
}
} finally {
DeathRow.unlock();
}
System.out.println("Outer time: " + (System.currentTimeMillis() - cyBase));
// Let workers quiesce/exit.
try {
Thread.sleep(1000);
} catch (Exception ex) {
}
;
}
use of java.util.concurrent.locks.ReentrantLock in project lucene-solr by apache.
the class TestDocumentsWriterDeleteQueue method testPartiallyAppliedGlobalSlice.
public void testPartiallyAppliedGlobalSlice() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InterruptedException {
final DocumentsWriterDeleteQueue queue = new DocumentsWriterDeleteQueue();
ReentrantLock lock = queue.globalBufferLock;
lock.lock();
Thread t = new Thread() {
@Override
public void run() {
queue.addDelete(new Term("foo", "bar"));
}
};
t.start();
t.join();
lock.unlock();
assertTrue("changes in del queue but not in slice yet", queue.anyChanges());
queue.tryApplyGlobalSlice();
assertTrue("changes in global buffer", queue.anyChanges());
FrozenBufferedUpdates freezeGlobalBuffer = queue.freezeGlobalBuffer(null);
assertTrue(freezeGlobalBuffer.any());
assertEquals(1, freezeGlobalBuffer.terms.size());
assertFalse("all changes applied", queue.anyChanges());
}
use of java.util.concurrent.locks.ReentrantLock in project geode by apache.
the class ForceableLinkedBlockingQueue method poll.
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
E x = null;
int c = -1;
long nanos = unit.toNanos(timeout);
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
while (count.get() == 0) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
x = dequeue();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}
use of java.util.concurrent.locks.ReentrantLock in project geode by apache.
the class ForceableLinkedBlockingQueue method offer.
/**
* Inserts the specified element at the tail of this queue if it is possible to do so immediately
* without exceeding the queue's capacity, returning {@code true} upon success and {@code false}
* if this queue is full. When using a capacity-restricted queue, this method is generally
* preferable to method {@link BlockingQueue#add add}, which can fail to insert an element only by
* throwing an exception.
*
* @throws NullPointerException if the specified element is null
*/
public boolean offer(E e) {
if (e == null)
throw new NullPointerException();
final AtomicInteger count = this.count;
if (// GEMFIRE changed == to >=
count.get() >= capacity)
return false;
int c = -1;
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
if (count.get() < capacity) {
enqueue(e);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
}
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
return c >= 0;
}
Aggregations