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

The following are Jave code examples for showing how to use unlock() 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: dhus-core   File: FairQueue.java   View Source Code Vote up 6 votes
@Override
public void put (E e) throws InterruptedException
{
   if (e == null)
   {
      throw new NullPointerException ();
   }

   int c = -1;
   final ReentrantLock putLock = this.putLock;
   final AtomicInteger count = this.count;
   putLock.lockInterruptibly ();
   try
   {
      store (e);
      c = count.getAndIncrement ();
   }
   finally
   {
      putLock.unlock ();
   }
   if (c == 0)
   {
      signalNotEmpty ();
   }
}
 
Example 2
Project: mycat-src-1.6.1-RELEASE   File: FixedQueue.java   View Source Code Vote up 6 votes
public void clear() {
    final E[] items = this.items;
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        int i = takeIndex;
        int j = count;
        while (j-- > 0) {
            items[i] = null;
            i = inc(i);
        }
        count = 0;
        putIndex = 0;
        takeIndex = 0;
    } finally {
        lock.unlock();
    }
}
 
Example 3
Project: OpenJSharp   File: LinkedBlockingQueue.java   View Source Code Vote up 6 votes
/**
 * Creates a {@code LinkedBlockingQueue} with a capacity of
 * {@link Integer#MAX_VALUE}, initially containing the elements of the
 * given collection,
 * added in traversal order of the collection's iterator.
 *
 * @param c the collection of elements to initially contain
 * @throws NullPointerException if the specified collection or any
 *         of its elements are null
 */
public LinkedBlockingQueue(Collection<? extends E> c) {
    this(Integer.MAX_VALUE);
    final ReentrantLock putLock = this.putLock;
    putLock.lock(); // Never contended, but necessary for visibility
    try {
        int n = 0;
        for (E e : c) {
            if (e == null)
                throw new NullPointerException();
            if (n == capacity)
                throw new IllegalStateException("Queue full");
            enqueue(new Node<E>(e));
            ++n;
        }
        count.set(n);
    } finally {
        putLock.unlock();
    }
}
 
Example 4
Project: dble   File: FairLinkedBlockingDeque.java   View Source Code Vote up 6 votes
public boolean removeFirstOccurrence(Object o) {
    if (o == null)
        return false;
    final ReentrantLock reentrantLock = this.lock;
    reentrantLock.lock();
    try {
        for (Node<E> p = first; p != null; p = p.next) {
            if (o.equals(p.item)) {
                unlink(p);
                return true;
            }
        }
        return false;
    } finally {
        reentrantLock.unlock();
    }
}
 
Example 5
Project: openjdk-jdk10   File: ArrayBlockingQueue.java   View Source Code Vote up 6 votes
/**
 * 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.  This method is generally preferable to method {@link #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) {
    Objects.requireNonNull(e);
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        if (count == items.length)
            return false;
        else {
            enqueue(e);
            return true;
        }
    } finally {
        lock.unlock();
    }
}
 
Example 6
Project: boohee_v5.6   File: LinkedBlockingDeque.java   View Source Code Vote up 6 votes
public void clear() {
    ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Node<E> f = this.first;
        while (f != null) {
            f.item = null;
            Node<E> n = f.next;
            f.prev = null;
            f.next = null;
            f = n;
        }
        this.last = null;
        this.first = null;
        this.count = 0;
        this.notFull.signalAll();
    } finally {
        lock.unlock();
    }
}
 
Example 7
Project: OpenJSharp   File: LinkedBlockingDeque.java   View Source Code Vote up 6 votes
/**
 * @throws NullPointerException {@inheritDoc}
 * @throws InterruptedException {@inheritDoc}
 */
public boolean offerLast(E e, long timeout, TimeUnit unit)
    throws InterruptedException {
    if (e == null) throw new NullPointerException();
    Node<E> node = new Node<E>(e);
    long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        while (!linkLast(node)) {
            if (nanos <= 0)
                return false;
            nanos = notFull.awaitNanos(nanos);
        }
        return true;
    } finally {
        lock.unlock();
    }
}
 
Example 8
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 9
Project: jdk8u-jdk   File: CopyOnWriteArrayList.java   View Source Code Vote up 5 votes
public List<E> subList(int fromIndex, int toIndex) {
    final ReentrantLock lock = l.lock;
    lock.lock();
    try {
        checkForComodification();
        if (fromIndex < 0 || toIndex > size || fromIndex > toIndex)
            throw new IndexOutOfBoundsException();
        return new COWSubList<E>(l, fromIndex + offset,
                                 toIndex + offset);
    } finally {
        lock.unlock();
    }
}
 
Example 10
Project: android-project-gallery   File: LinkedBlockingDeque.java   View Source Code Vote up 5 votes
/**
 * @throws NullPointerException {@inheritDoc}
 */
public boolean offerLast(E e) {
    if (e == null) throw new NullPointerException();
    Node<E> node = new Node<E>(e);
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return linkLast(node);
    } finally {
        lock.unlock();
    }
}
 
Example 11
Project: letv   File: LinkedBlockingDeque.java   View Source Code Vote up 5 votes
public E peekLast() {
    ReentrantLock lock = this.lock;
    lock.lock();
    try {
        E e = this.last == null ? null : this.last.item;
        lock.unlock();
        return e;
    } catch (Throwable th) {
        lock.unlock();
    }
}
 
Example 12
Project: android-retrostreams   File: LBDSpliterator.java   View Source Code Vote up 5 votes
@Override
public Spliterator<E> trySplit() {
    Object h;
    LinkedBlockingDeque<E> q = queue;
    if (!exhausted &&
        ((h = current) != null || (h = getQueueFirst(q)) != null)
        && getNextNode(h) != null) {
        int n = batch = Math.min(batch + 1, MAX_BATCH);
        Object[] a = new Object[n];
        ReentrantLock lock = queueLock;
        int i = 0;
        Object p = current;
        lock.lock();
        try {
            if (p != null || (p = getQueueFirst(q)) != null)
                for (; p != null && i < n; p = succ(p))
                    if ((a[i] = getNodeItem(p)) != null)
                        i++;
        } finally {
            // checkInvariants();
            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 13
Project: letv   File: LinkedBlockingDeque.java   View Source Code Vote up 5 votes
public E pollFirst() {
    ReentrantLock lock = this.lock;
    lock.lock();
    try {
        E unlinkFirst = unlinkFirst();
        return unlinkFirst;
    } finally {
        lock.unlock();
    }
}
 
Example 14
Project: OpenJSharp   File: CopyOnWriteArrayList.java   View Source Code Vote up 5 votes
public E get(int index) {
    final ReentrantLock lock = l.lock;
    lock.lock();
    try {
        rangeCheck(index);
        checkForComodification();
        return l.get(index+offset);
    } finally {
        lock.unlock();
    }
}
 
Example 15
Project: tomcat7   File: FairBlockingQueue.java   View Source Code Vote up 5 votes
/**
 * {@inheritDoc}
 */
@Override
public E poll() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return items.poll();
    } finally {
        lock.unlock();
    }
}
 
Example 16
Project: nymph   File: NyQueue.java   View Source Code Vote up 5 votes
/** 
 * 往队列尾部插入元素,容量最大值时等待 
 * @return 
 * @throws InterruptedException 
 */
public void put(E e) throws InterruptedException {
       int c = -1;
       final ReentrantLock putLock = this.putLock;
       final AtomicInteger count = this.size;
       putLock.lockInterruptibly();
       try {
           /*
            * Note that count is used in wait guard even though it is
            * not protected by lock. This works because count can
            * only decrease at this point (all other puts are shut
            * out by lock), and we (or some other waiting put) are
            * signalled if it ever changes from capacity. Similarly
            * for all other uses of count in other wait guards.
            */
           while (count.get() == Integer.MAX_VALUE) {
               notFull.await();
           }
           super.offer(e);
           c = count.getAndIncrement();
           if (c + 1 < Integer.MAX_VALUE)
               notFull.signal();
       } finally {
           putLock.unlock();
       }
       if (c == 0)
           signalNotEmpty();
}
 
Example 17
Project: boohee_v5.6   File: LinkedBlockingDeque.java   View Source Code Vote up 5 votes
void advance() {
    ReentrantLock lock = LinkedBlockingDeque.this.lock;
    lock.lock();
    try {
        this.next = succ(this.next);
        this.nextItem = this.next == null ? null : this.next.item;
    } finally {
        lock.unlock();
    }
}
 
Example 18
Project: GitHub   File: LinkedBlockingDeque.java   View Source Code Vote up 5 votes
public E takeLast() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        E x;
        while ((x = unlinkLast()) == null)
            notEmpty.await();
        return x;
    } finally {
        lock.unlock();
    }
}
 
Example 19
Project: util   File: ForkJoinTask.java   View Source Code Vote up 4 votes
/**
 * Returns a rethrowable exception for the given task, if available. To
 * provide accurate stack traces, if the exception was not thrown by the
 * current thread, we try to create a new exception of the same type as the
 * one thrown, but with the recorded exception as its cause. If there is no
 * such constructor, we instead try to use a no-arg constructor, followed by
 * initCause, to the same effect. If none of these apply, or any fail due to
 * other exceptions, we return the recorded exception, which is still
 * correct, although it may contain a misleading stack trace.
 *
 * @return the exception, or null if none
 */
@SuppressWarnings("unused")
private Throwable getThrowableException() {
	if ((status & DONE_MASK) != EXCEPTIONAL)
		return null;
	int h = System.identityHashCode(this);
	ExceptionNode e;
	final ReentrantLock lock = exceptionTableLock;
	lock.lock();
	try {
		expungeStaleExceptions();
		ExceptionNode[] t = exceptionTable;
		e = t[h & (t.length - 1)];
		while (e != null && e.get() != this)
			e = e.next;
	} finally {
		lock.unlock();
	}
	Throwable ex;
	if (e == null || (ex = e.ex) == null)
		return null;
	if (false && e.thrower != Thread.currentThread().getId()) {
		Class<? extends Throwable> ec = ex.getClass();
		try {
			Constructor<?> noArgCtor = null;
			Constructor<?>[] cs = ec.getConstructors();// public ctors only
			for (int i = 0; i < cs.length; ++i) {
				Constructor<?> c = cs[i];
				Class<?>[] ps = c.getParameterTypes();
				if (ps.length == 0)
					noArgCtor = c;
				else if (ps.length == 1 && ps[0] == Throwable.class)
					return (Throwable) (c.newInstance(ex));
			}
			if (noArgCtor != null) {
				Throwable wx = (Throwable) (noArgCtor.newInstance());
				wx.initCause(ex);
				return wx;
			}
		} catch (Exception ignore) {
		}
	}
	return ex;
}
 
Example 20
Project: mycat-src-1.6.1-RELEASE   File: MycatConfig.java   View Source Code Vote up 4 votes
private void apply(Map<String, UserConfig> newUsers,
		Map<String, SchemaConfig> newSchemas,
		Map<String, PhysicalDBNode> newDataNodes,
		Map<String, PhysicalDBPool> newDataHosts, 
		MycatCluster newCluster,
		FirewallConfig newFirewall,
		boolean isLoadAll) {
	
	final ReentrantLock lock = this.lock;
	lock.lock();
	try {
		
		// old 处理
		// 1、停止老的数据源心跳
		// 2、备份老的数据源配置
		//--------------------------------------------
		if (isLoadAll) {				
			Map<String, PhysicalDBPool> oldDataHosts = this.dataHosts;
			if (oldDataHosts != null) {
				for (PhysicalDBPool oldDbPool : oldDataHosts.values()) {
					if (oldDbPool != null) {
						oldDbPool.stopHeartbeat();
					}
				}
			}
			this._dataNodes = this.dataNodes;
			this._dataHosts = this.dataHosts;
		}
		
		this._users = this.users;
		this._schemas = this.schemas;
		this._cluster = this.cluster;
		this._firewall = this.firewall;

		// new 处理
		// 1、启动新的数据源心跳
		// 2、执行新的配置
		//---------------------------------------------------
		if (isLoadAll) {
			if (newDataHosts != null) {
				for (PhysicalDBPool newDbPool : newDataHosts.values()) {
					if ( newDbPool != null) {
						newDbPool.startHeartbeat();
					}
				}
			}
			this.dataNodes = newDataNodes;
			this.dataHosts = newDataHosts;
		}			
		this.users = newUsers;
		this.schemas = newSchemas;
		this.cluster = newCluster;
		this.firewall = newFirewall;
		
	} finally {
		lock.unlock();
	}
}