Java Code Examples for java.util.concurrent.locks.LockSupport#unpark()

The following examples show how to use java.util.concurrent.locks.LockSupport#unpark() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FutureTask.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Removes and signals all waiting threads, invokes done(), and
 * nulls out callable.
 */
private void finishCompletion() {
    // assert state > COMPLETING;
    for (WaitNode q; (q = waiters) != null;) {
        if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {
            for (;;) {
                Thread t = q.thread;
                if (t != null) {
                    q.thread = null;
                    LockSupport.unpark(t);
                }
                WaitNode next = q.next;
                if (next == null)
                    break;
                q.next = null; // unlink to help gc
                q = next;
            }
            break;
        }
    }

    done();

    callable = null;        // to reduce footprint
}
 
Example 2
Source File: AbstractFuture.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void unpark() {
  // This is racy with removeWaiter. The consequence of the race is that we may spuriously call
  // unpark even though the thread has already removed itself from the list. But even if we did
  // use a CAS, that race would still exist (it would just be ever so slightly smaller).
  Thread w = thread;
  if (w != null) {
    thread = null;
    LockSupport.unpark(w);
  }
}
 
Example 3
Source File: DistributedReentrantLock.java    From dlock with Apache License 2.0 5 votes vote down vote up
/**
 * wake up the head node for compete lock
 */
private void unparkQueuedNode() {
    // wake up the head node for compete lock
    Node h = head.get();
    if (h != null && h.next.get() != null) {
        LockSupport.unpark(h.next.get().t);
    }
}
 
Example 4
Source File: LinkedTransferQueue.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tries to artificially match a data node -- used by remove.
 */
final boolean tryMatchData() {
    // assert isData;
    Object x = item;
    if (x != null && x != this && casItem(x, null)) {
        LockSupport.unpark(waiter);
        return true;
    }
    return false;
}
 
Example 5
Source File: InternalThreadLocalTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
/**
 * print
 * take[2689]ms
 * <p></p>
 * This test is based on a Machine with 4 core and 16g memory.
 */
@Test
public void testPerformanceTradition() {
    final ThreadLocal<String>[] caches1 = new ThreadLocal[PERFORMANCE_THREAD_COUNT];
    final Thread mainThread = Thread.currentThread();
    for (int i = 0; i < PERFORMANCE_THREAD_COUNT; i++) {
        caches1[i] = new ThreadLocal<String>();
    }
    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < PERFORMANCE_THREAD_COUNT; i++) {
                caches1[i].set("float.lu");
            }
            long start = System.nanoTime();
            for (int i = 0; i < PERFORMANCE_THREAD_COUNT; i++) {
                for (int j = 0; j < GET_COUNT; j++) {
                    caches1[i].get();
                }
            }
            long end = System.nanoTime();
            System.out.println("take[" + TimeUnit.NANOSECONDS.toMillis(end - start) +
                    "]ms");
            LockSupport.unpark(mainThread);
        }
    });
    t1.start();
    LockSupport.park(mainThread);
}
 
Example 6
Source File: LinkedTransferQueue.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Tries to CAS-match this node; if successful, wakes waiter. */
final boolean tryMatch(Object cmp, Object val) {
    if (casItem(cmp, val)) {
        LockSupport.unpark(waiter);
        return true;
    }
    return false;
}
 
Example 7
Source File: ParkLoops.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    for (int n = 0; (n++ & 0xff) != 0 || done.getCount() > 0;) {
        int j = rnd.nextInt(THREADS);
        Thread parker = threads.get(j);
        if (parker != null &&
            threads.compareAndSet(j, parker, null)) {
            LockSupport.unpark(parker);
        }
    }
}
 
Example 8
Source File: CompletableFuture.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
final CompletableFuture<?> tryFire(int ignore) {
    Thread w; // no need to atomically claim
    if ((w = thread) != null) {
        thread = null;
        LockSupport.unpark(w);
    }
    return null;
}
 
Example 9
Source File: Gate.java    From x-pipe with Apache License 2.0 5 votes vote down vote up
private void makeAllPass() {

        Thread passenger;
        while ((passenger = passengers.poll()) != null) {
            logger.info("[makeAllPass][{}], {}", name, passenger);
            LockSupport.unpark(passenger);
        }
    }
 
Example 10
Source File: UnfairExecutor.java    From threadly with Mozilla Public License 2.0 5 votes vote down vote up
public void addTask(Runnable task) {
  taskQueue.add(task);
  if (parked) {
    parked = false;
    LockSupport.unpark(thread);
  } else if (wakupNeighborWorker.parked) {
    wakupNeighborWorker.parked = false;
    LockSupport.unpark(wakupNeighborWorker.thread);
  }
}
 
Example 11
Source File: SynchronousQueueNoSpin.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to match node s to this node, if so, waking up thread.
 * Fulfillers call tryMatch to identify their waiters.
 * Waiters block until they have been matched.
 *
 * @param s the node to match
 * @return true if successfully matched to s
 */
boolean tryMatch(SNode s) {
    if (match == null &&
        matchUpdater.compareAndSet(this, null, s)) {
        Thread w = waiter;
        if (w != null) {    // waiters need at most one unpark
            waiter = null;
            LockSupport.unpark(w);
        }
        return true;
    }
    return match == s;
}
 
Example 12
Source File: SynchronousQueue.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tries to match node s to this node, if so, waking up thread.
 * Fulfillers call tryMatch to identify their waiters.
 * Waiters block until they have been matched.
 *
 * @param s the node to match
 * @return true if successfully matched to s
 */
boolean tryMatch(SNode s) {
    if (match == null &&
        UNSAFE.compareAndSwapObject(this, matchOffset, null, s)) {
        Thread w = waiter;
        if (w != null) {    // waiters need at most one unpark
            waiter = null;
            LockSupport.unpark(w);
        }
        return true;
    }
    return match == s;
}
 
Example 13
Source File: TestLockSupport.java    From code with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    t1.start();
    Thread.sleep(1000);
    t2.start();
    LockSupport.unpark(t1);
    Thread.sleep(1000);
    LockSupport.unpark(t2);
    t1.join();
    t2.join();
}
 
Example 14
Source File: Phaser.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Variant of releaseWaiters that additionally tries to remove any
 * nodes no longer waiting for advance due to timeout or
 * interrupt. Currently, nodes are removed only if they are at
 * head of queue, which suffices to reduce memory footprint in
 * most usages.
 *
 * @return current phase on exit
 */
private int abortWait(int phase) {
    AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ;
    for (;;) {
        Thread t;
        QNode q = head.get();
        int p = (int)(root.state >>> PHASE_SHIFT);
        if (q == null || ((t = q.thread) != null && q.phase == p))
            return p;
        if (head.compareAndSet(q, q.next) && t != null) {
            q.thread = null;
            LockSupport.unpark(t);
        }
    }
}
 
Example 15
Source File: Phaser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Variant of releaseWaiters that additionally tries to remove any
 * nodes no longer waiting for advance due to timeout or
 * interrupt. Currently, nodes are removed only if they are at
 * head of queue, which suffices to reduce memory footprint in
 * most usages.
 *
 * @return current phase on exit
 */
private int abortWait(int phase) {
    AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ;
    for (;;) {
        Thread t;
        QNode q = head.get();
        int p = (int)(root.state >>> PHASE_SHIFT);
        if (q == null || ((t = q.thread) != null && q.phase == p))
            return p;
        if (head.compareAndSet(q, q.next) && t != null) {
            q.thread = null;
            LockSupport.unpark(t);
        }
    }
}
 
Example 16
Source File: SingleConsumerCondition.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public void signal() {
  LockSupport.unpark(consumer);
}
 
Example 17
Source File: RabbitMQConsumer.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void handleCancel(String consumerTag) throws IOException {
    LockSupport.unpark(listenerThread);
}
 
Example 18
Source File: XActorThread.java    From actor4j-core with Apache License 2.0 4 votes vote down vote up
@Override
protected void newMessage() {
	if (system.threadMode==ActorThreadMode.PARK && newMessage.compareAndSet(false, true))
		LockSupport.unpark(this);
}
 
Example 19
Source File: FIFOMutex.java    From PoseidonX with Apache License 2.0 4 votes vote down vote up
/**
 * <取消锁定>
 */
public void unlock()
{
    locked.set(false);
    LockSupport.unpark(waiters.peek());
}
 
Example 20
Source File: ReSendMessageService.java    From jmqtt with Apache License 2.0 4 votes vote down vote up
public void wakeUp(){
    LockSupport.unpark(thread);
}