Java Code Examples for java.util.concurrent.locks.ReentrantLock.lock()

The following are Jave code examples for showing how to use lock() of the java.util.concurrent.locks.ReentrantLock class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: GifImageLoader   File: LinkedBlockingDeque.java   View Source Code Vote up 6 votes
public String toString() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Node<E> p = first;
        if (p == null)
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (; ; ) {
            E e = p.item;
            sb.append(e == this ? "(this Collection)" : e);
            p = p.next;
            if (p == null)
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    } finally {
        lock.unlock();
    }
}
 
Example 2
Project: boohee_v5.6   File: LinkedBlockingDeque.java   View Source Code Vote up 6 votes
public LinkedBlockingDeque(Collection<? extends E> c) {
    this((int) ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED);
    ReentrantLock lock = this.lock;
    lock.lock();
    try {
        for (E e : c) {
            if (e == null) {
                throw new NullPointerException();
            } else if (!linkLast(new Node(e))) {
                throw new IllegalStateException("Deque full");
            }
        }
    } finally {
        lock.unlock();
    }
}
 
Example 3
Project: letv   File: LinkedBlockingDeque.java   View Source Code Vote up 6 votes
public LinkedBlockingDeque(Collection<? extends E> c) {
    this(Integer.MAX_VALUE);
    ReentrantLock lock = this.lock;
    lock.lock();
    try {
        for (E e : c) {
            if (e == null) {
                throw new NullPointerException();
            } else if (!linkLast(new Node(e))) {
                throw new IllegalStateException("Deque full");
            }
        }
    } finally {
        lock.unlock();
    }
}
 
Example 4
Project: openjdk-jdk10   File: ScheduledThreadPoolExecutor.java   View Source Code Vote up 6 votes
public void clear() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        for (int i = 0; i < size; i++) {
            RunnableScheduledFuture<?> t = queue[i];
            if (t != null) {
                queue[i] = null;
                setIndex(t, -1);
            }
        }
        size = 0;
    } finally {
        lock.unlock();
    }
}
 
Example 5
Project: jdk8u-jdk   File: LinkedBlockingQueue.java   View Source Code Vote up 6 votes
public E poll() {
    final AtomicInteger count = this.count;
    if (count.get() == 0)
        return null;
    E x = null;
    int c = -1;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        if (count.get() > 0) {
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        }
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 6
Project: GifImageLoader   File: LinkedBlockingDeque.java   View Source Code Vote up 6 votes
/**
 * @throws UnsupportedOperationException {@inheritDoc}
 * @throws ClassCastException            {@inheritDoc}
 * @throws NullPointerException          {@inheritDoc}
 * @throws IllegalArgumentException      {@inheritDoc}
 */
public int drainTo(Collection<? super E> c, int maxElements) {
    if (c == null)
        throw new NullPointerException();
    if (c == this)
        throw new IllegalArgumentException();
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        int n = Math.min(maxElements, count);
        for (int i = 0; i < n; i++) {
            c.add(first.item);   // In this order, in case add() throws.
            unlinkFirst();
        }
        return n;
    } finally {
        lock.unlock();
    }
}
 
Example 7
Project: jdk8u-jdk   File: CopyOnWriteArrayList.java   View Source Code Vote up 6 votes
public void replaceAll(UnaryOperator<E> operator) {
    if (operator == null) throw new NullPointerException();
    final ReentrantLock lock = l.lock;
    lock.lock();
    try {
        int lo = offset;
        int hi = offset + size;
        Object[] elements = expectedArray;
        if (l.getArray() != elements)
            throw new ConcurrentModificationException();
        int len = elements.length;
        if (lo < 0 || hi > len)
            throw new IndexOutOfBoundsException();
        Object[] newElements = Arrays.copyOf(elements, len);
        for (int i = lo; i < hi; ++i) {
            @SuppressWarnings("unchecked") E e = (E) elements[i];
            newElements[i] = operator.apply(e);
        }
        l.setArray(expectedArray = newElements);
    } finally {
        lock.unlock();
    }
}
 
Example 8
Project: android-project-gallery   File: LinkedBlockingDeque.java   View Source Code Vote up 6 votes
public boolean removeLastOccurrence(Object o) {
    if (o == null) return false;
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        for (Node<E> p = last; p != null; p = p.prev) {
            if (o.equals(p.item)) {
                unlink(p);
                return true;
            }
        }
        return false;
    } finally {
        lock.unlock();
    }
}
 
Example 9
Project: GitHub   File: PriorityBlockingQueue.java   View Source Code Vote up 6 votes
public E poll() {
    final AtomicInteger count = this.count;
    if (count.get() == 0) return null;
    E x = null;
    int c = -1;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        if (count.get() > 0) {
            x = opQueue(null);
            c = count.getAndDecrement();
            if (c > 1) notEmpty.signal();
        }
    } finally {
        takeLock.unlock();
    }
    if (c == capacity) signalNotFull();
    return x;
}
 
Example 10
Project: GitHub   File: LinkedBlockingDeque.java   View Source Code Vote up 6 votes
public boolean removeLastOccurrence(Object o) {
    if (o == null) return false;
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        for (Node<E> p = last; p != null; p = p.prev) {
            if (o.equals(p.item)) {
                unlink(p);
                return true;
            }
        }
        return false;
    } finally {
        lock.unlock();
    }
}
 
Example 11
Project: boohee_v5.6   File: LinkedBlockingDeque.java   View Source Code Vote up 6 votes
public int drainTo(Collection<? super E> c, int maxElements) {
    if (c == null) {
        throw new NullPointerException();
    } else if (c == this) {
        throw new IllegalArgumentException();
    } else {
        ReentrantLock lock = this.lock;
        lock.lock();
        try {
            int n = Math.min(maxElements, this.count);
            for (int i = 0; i < n; i++) {
                c.add(this.first.item);
                unlinkFirst();
            }
            return n;
        } finally {
            lock.unlock();
        }
    }
}
 
Example 12
Project: tomcat7   File: FairBlockingQueue.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked") // Can't create arrays of generic types
public FairIterator() {
    final ReentrantLock lock = FairBlockingQueue.this.lock;
    lock.lock();
    try {
        elements = (E[]) new Object[FairBlockingQueue.this.items.size()];
        FairBlockingQueue.this.items.toArray(elements);
        index = 0;
    } finally {
        lock.unlock();
    }
}
 
Example 13
Project: jdk8u-jdk   File: ThreadPoolExecutor.java   View Source Code Vote up 5 votes
/**
 * Rolls back the worker thread creation.
 * - removes worker from workers, if present
 * - decrements worker count
 * - rechecks for termination, in case the existence of this
 *   worker was holding up termination
 */
private void addWorkerFailed(Worker w) {
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        if (w != null)
            workers.remove(w);
        decrementWorkerCount();
        tryTerminate();
    } finally {
        mainLock.unlock();
    }
}
 
Example 14
Project: jdk8u-jdk   File: LinkedBlockingDeque.java   View Source Code Vote up 5 votes
public Spliterator<E> trySplit() {
    Node<E> h;
    final LinkedBlockingDeque<E> q = this.queue;
    int b = batch;
    int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
    if (!exhausted &&
        ((h = current) != null || (h = q.first) != null) &&
        h.next != null) {
        Object[] a = new Object[n];
        final ReentrantLock lock = q.lock;
        int i = 0;
        Node<E> p = current;
        lock.lock();
        try {
            if (p != null || (p = q.first) != null) {
                do {
                    if ((a[i] = p.item) != null)
                        ++i;
                } while ((p = p.next) != null && i < n);
            }
        } finally {
            lock.unlock();
        }
        if ((current = p) == null) {
            est = 0L;
            exhausted = true;
        }
        else if ((est -= i) < 0L)
            est = 0L;
        if (i > 0) {
            batch = i;
            return Spliterators.spliterator
                (a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
                 Spliterator.CONCURRENT);
        }
    }
    return null;
}
 
Example 15
Project: GitHub   File: LinkedBlockingDeque.java   View Source Code Vote up 5 votes
/**
 * @throws NullPointerException {@inheritDoc}
 * @throws InterruptedException {@inheritDoc}
 */
public void putFirst(E e) throws InterruptedException {
    if (e == null) throw new NullPointerException();
    Node<E> node = new Node<E>(e);
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        while (!linkFirst(node))
            notFull.await();
    } finally {
        lock.unlock();
    }
}
 
Example 16
Project: GitHub   File: LinkedBlockingDeque.java   View Source Code Vote up 5 votes
public E pollLast() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return unlinkLast();
    } finally {
        lock.unlock();
    }
}
 
Example 17
Project: tomcat7   File: FairBlockingQueue.java   View Source Code Vote up 5 votes
/**
 * {@inheritDoc}
 */
@Override
public boolean contains(Object e) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return items.contains(e);
    } finally {
        lock.unlock();
    }
}
 
Example 18
Project: openjdk-jdk10   File: LinkedBlockingDeque.java   View Source Code Vote up 5 votes
public Spliterator<E> trySplit() {
    Node<E> h;
    if (!exhausted &&
        ((h = current) != null || (h = first) != null)
        && h.next != null) {
        int n = batch = Math.min(batch + 1, MAX_BATCH);
        Object[] a = new Object[n];
        final ReentrantLock lock = LinkedBlockingDeque.this.lock;
        int i = 0;
        Node<E> p = current;
        lock.lock();
        try {
            if (p != null || (p = first) != null)
                for (; p != null && i < n; p = succ(p))
                    if ((a[i] = p.item) != null)
                        i++;
        } finally {
            lock.unlock();
        }
        if ((current = p) == null) {
            est = 0L;
            exhausted = true;
        }
        else if ((est -= i) < 0L)
            est = 0L;
        if (i > 0)
            return Spliterators.spliterator
                (a, 0, i, (Spliterator.ORDERED |
                           Spliterator.NONNULL |
                           Spliterator.CONCURRENT));
    }
    return null;
}
 
Example 19
Project: dble   File: FairLinkedBlockingDeque.java   View Source Code Vote up 5 votes
/**
 * @throws NullPointerException {@inheritDoc}
 */
public boolean offerLast(E e) {
    if (e == null)
        throw new NullPointerException();
    final ReentrantLock reentrantLock = this.lock;
    reentrantLock.lock();
    try {
        return linkLast(e);
    } finally {
        reentrantLock.unlock();
    }
}
 
Example 20
Project: GifImageLoader   File: LinkedBlockingDeque.java   View Source Code Vote up 5 votes
AbstractItr() {
    // set to initial position
    final ReentrantLock lock = LinkedBlockingDeque.this.lock;
    lock.lock();
    try {
        next = firstNode();
        nextItem = (next == null) ? null : next.item;
    } finally {
        lock.unlock();
    }
}