Search in sources :

Example 56 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by DirtyUnicorns.

the class CaptureCollector method failAll.

/**
     * Called to alert the {@link CaptureCollector} all pending captures have failed.
     */
public void failAll() {
    final ReentrantLock lock = this.mLock;
    lock.lock();
    try {
        CaptureHolder h;
        while ((h = mActiveRequests.pollFirst()) != null) {
            h.setPreviewFailed();
            h.setJpegFailed();
        }
        mPreviewCaptureQueue.clear();
        mPreviewProduceQueue.clear();
        mJpegCaptureQueue.clear();
        mJpegProduceQueue.clear();
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 57 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by DirtyUnicorns.

the class CaptureCollector method previewCaptured.

/**
     * Called to alert the {@link CaptureCollector} that the preview capture has begun.
     *
     * @param timestamp the time of the preview capture.
     * @return a pair containing the {@link RequestHolder} and the timestamp of the capture.
     */
public Pair<RequestHolder, Long> previewCaptured(long timestamp) {
    final ReentrantLock lock = this.mLock;
    lock.lock();
    try {
        CaptureHolder h = mPreviewCaptureQueue.poll();
        if (h == null) {
            if (DEBUG) {
                Log.d(TAG, "previewCaptured called with no preview request on queue!");
            }
            return null;
        }
        h.setPreviewTimestamp(timestamp);
        return new Pair<>(h.mRequest, h.mTimestamp);
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Pair(android.util.Pair)

Example 58 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by DirtyUnicorns.

the class RenderAction method release.

/**
     * Cleans up the scene after an action.
     */
public void release() {
    ReentrantLock lock = Bridge.getLock();
    // not throw IllegalMonitorStateException.
    if (lock.isHeldByCurrentThread()) {
        tearDown();
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 59 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project canal by alibaba.

the class MemoryEventStoreWithBuffer method cleanUntil.

public void cleanUntil(Position position) throws CanalStoreException {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        long sequence = ackSequence.get();
        long maxSequence = getSequence.get();
        boolean hasMatch = false;
        long memsize = 0;
        for (long next = sequence + 1; next <= maxSequence; next++) {
            Event event = entries[getIndex(next)];
            memsize += calculateSize(event);
            boolean match = CanalEventUtils.checkPosition(event, (LogPosition) position);
            if (match) {
                // 找到对应的position,更新ack seq
                hasMatch = true;
                if (batchMode.isMemSize()) {
                    ackMemSize.addAndGet(memsize);
                    // 尝试清空buffer中的内存,将ack之前的内存全部释放掉
                    for (long index = sequence + 1; index < next; index++) {
                        // 设置为null
                        entries[getIndex(index)] = null;
                    }
                }
                if (ackSequence.compareAndSet(sequence, next)) {
                    // 避免并发ack
                    notFull.signal();
                    return;
                }
            }
        }
        if (!hasMatch) {
            // 找不到对应需要ack的position
            throw new CanalStoreException("no match ack position" + position.toString());
        }
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Event(com.alibaba.otter.canal.store.model.Event) CanalStoreException(com.alibaba.otter.canal.store.CanalStoreException)

Example 60 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project intellij-community by JetBrains.

the class ConcurrencyUtil method awaitQuiescence.

/**
   * Awaits for all tasks in the {@code executor} to finish for the specified {@code timeout}
   */
@TestOnly
public static void awaitQuiescence(@NotNull ThreadPoolExecutor executor, long timeout, @NotNull TimeUnit unit) {
    // no need for zombies in tests
    executor.setKeepAliveTime(1, TimeUnit.NANOSECONDS);
    // interrupt idle workers
    executor.setCorePoolSize(0);
    ReentrantLock mainLock = ReflectionUtil.getField(executor.getClass(), executor, ReentrantLock.class, "mainLock");
    Set workers;
    mainLock.lock();
    try {
        HashSet workersField = ReflectionUtil.getField(executor.getClass(), executor, HashSet.class, "workers");
        // to be able to iterate thread-safely outside the lock
        workers = new HashSet(workersField);
    } finally {
        mainLock.unlock();
    }
    for (Object worker : workers) {
        Thread thread = ReflectionUtil.getField(worker.getClass(), worker, Thread.class, "thread");
        try {
            thread.join(unit.toMillis(timeout));
        } catch (InterruptedException e) {
            String trace = "Thread leaked: " + thread + "; " + thread.getState() + " (" + thread.isAlive() + ")\n--- its stacktrace:\n";
            for (final StackTraceElement stackTraceElement : thread.getStackTrace()) {
                trace += " at " + stackTraceElement + "\n";
            }
            trace += "---\n";
            System.err.println("Executor " + executor + " is still active after " + unit.toSeconds(timeout) + " seconds://///\n" + "Thread " + thread + " dump:\n" + trace + "all thread dump:\n" + ThreadDumper.dumpThreadsToString() + "\n/////");
            break;
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) TestOnly(org.jetbrains.annotations.TestOnly)

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