本文整理汇总了Java中java.util.concurrent.locks.ReentrantLock.lock方法的典型用法代码示例。如果您正苦于以下问题:Java ReentrantLock.lock方法的具体用法?Java ReentrantLock.lock怎么用?Java ReentrantLock.lock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.locks.ReentrantLock
的用法示例。
在下文中一共展示了ReentrantLock.lock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toString
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
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();
}
}
示例2: LinkedBlockingDeque
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
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();
}
}
示例3: LinkedBlockingDeque
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
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();
}
}
示例4: clear
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
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();
}
}
示例5: poll
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
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;
}
示例6: drainTo
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
* @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();
}
}
示例7: replaceAll
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
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();
}
}
示例8: removeLastOccurrence
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
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();
}
}
示例9: poll
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
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;
}
示例10: drainTo
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
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();
}
}
}
示例11: FairIterator
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
@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();
}
}
示例12: addWorkerFailed
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
* 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();
}
}
示例13: trySplit
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
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;
}
示例14: putFirst
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
* @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();
}
}
示例15: pollLast
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
public E pollLast() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return unlinkLast();
} finally {
lock.unlock();
}
}