Search in sources :

Example 61 with ReentrantLock

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();
}
Also used : Path(java.nio.file.Path) ReentrantLock(java.util.concurrent.locks.ReentrantLock) IOException(java.io.IOException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AccessDeniedException(java.nio.file.AccessDeniedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 62 with ReentrantLock

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) {
    }
    ;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 63 with ReentrantLock

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());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 64 with ReentrantLock

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;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 65 with ReentrantLock

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;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

ReentrantLock (java.util.concurrent.locks.ReentrantLock)1524 Lock (java.util.concurrent.locks.Lock)125 Test (org.junit.Test)78 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)74 Condition (java.util.concurrent.locks.Condition)57 ArrayList (java.util.ArrayList)45 IOException (java.io.IOException)42 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 File (java.io.File)22 TimeoutException (java.util.concurrent.TimeoutException)19 Before (org.junit.Before)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 HashMap (java.util.HashMap)16 CountDownLatch (java.util.concurrent.CountDownLatch)16 ExecutionException (java.util.concurrent.ExecutionException)15 List (java.util.List)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 Map (java.util.Map)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)11 Pair (android.util.Pair)10