本文整理汇总了Java中java.util.concurrent.locks.LockSupport.park方法的典型用法代码示例。如果您正苦于以下问题:Java LockSupport.park方法的具体用法?Java LockSupport.park怎么用?Java LockSupport.park使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.locks.LockSupport
的用法示例。
在下文中一共展示了LockSupport.park方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
@Override
public void run() {
synchronized (u) {
System.out.printf("%s in %s\n", new Date(), getName());
LockSupport.park(u);
if (isInterrupted()) {
System.out.println(getName() + " is interrupted.");
}
}
// 最多阻塞 5 s
// 如果中断位为 true,则下面的语句无效,可以对比上面如果是使用 Thread.interrupted() 方法判断有什么不同
LockSupport.parkNanos(this, TimeUnit.SECONDS.toNanos(5));
System.out.printf("%s %s ends\n", new Date(), getName());
}
示例2: testCircuitBreaker
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
@Test
public void testCircuitBreaker() {
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
.failureRateThreshold(25)
.waitDurationInOpenState(Duration.ofMillis(1000))
.ringBufferSizeInHalfOpenState(1)
.ringBufferSizeInClosedState(2)
.build();
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("test");
Observable.interval(500,TimeUnit.MILLISECONDS).map(i -> {
if(1==1) throw new RuntimeException("BAM");
return "result"+i;
})
.lift(CircuitBreakerOperator.of(circuitBreaker)).map(result -> result)
.subscribe(System.out::println);
LockSupport.park();
}
示例3: contendedLock
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
/**
* Possibly blocks awaiting root lock.
*/
private final void contendedLock() {
boolean waiting = false;
for (int s;;) {
if (((s = lockState) & ~WAITER) == 0) {
if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
if (waiting)
waiter = null;
return;
}
}
else if ((s & WAITER) == 0) {
if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
waiting = true;
waiter = Thread.currentThread();
}
}
else if (waiting)
LockSupport.park(this);
}
}
示例4: block
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
/**
* Blocks until closed, space available or timeout.
* For ManagedBlocker.
*/
public final boolean block() {
long nanos = timeout;
boolean timed = (nanos < Long.MAX_VALUE);
long deadline = timed ? System.nanoTime() + nanos : 0L;
while (!isReleasable()) {
if (Thread.interrupted()) {
timeout = INTERRUPTED;
if (timed)
break;
}
else if (timed && (nanos = deadline - System.nanoTime()) <= 0L)
break;
else if (waiter == null)
waiter = Thread.currentThread();
else if (waiting == 0)
waiting = 1;
else if (timed)
LockSupport.parkNanos(this, nanos);
else
LockSupport.park(this);
}
waiter = null;
waiting = 0;
return true;
}
示例5: await
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
public void await() throws InterruptedException
{
while (!isSignalled())
{
checkInterrupted();
LockSupport.park();
}
checkAndClear();
}
示例6: block
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
public boolean block() {
while (!isReleasable()) {
if (timed)
LockSupport.parkNanos(this, nanos);
else
LockSupport.park(this);
}
return true;
}
示例7: run
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
@Override
public void run() {
synchronized (u){
System.out.println("in "+getName());
LockSupport.park();
}
}
示例8: block
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
public boolean block() {
while (!isReleasable()) {
if (timed) {
LockSupport.parkNanos(this, nanos);
} else {
LockSupport.park(this);
}
}
return true;
}
示例9: get
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*
* <p>The default {@link AbstractFuture} implementation throws {@code InterruptedException} if the
* current thread is interrupted before or during the call, even if the value is already
* available.
*
* @throws InterruptedException if the current thread was interrupted before or during the call
* (optional but recommended).
* @throws CancellationException {@inheritDoc}
*/
@CanIgnoreReturnValue
@Override
public V get() throws InterruptedException, ExecutionException {
if (Thread.interrupted()) {
throw new InterruptedException();
}
Object localValue = value;
if (localValue != null & !(localValue instanceof SetFuture)) {
return getDoneValue(localValue);
}
Waiter oldHead = waiters;
if (oldHead != Waiter.TOMBSTONE) {
Waiter node = new Waiter();
do {
node.setNext(oldHead);
if (ATOMIC_HELPER.casWaiters(this, oldHead, node)) {
// we are on the stack, now wait for completion.
while (true) {
LockSupport.park(this);
// Check interruption first, if we woke up due to interruption we need to honor that.
if (Thread.interrupted()) {
removeWaiter(node);
throw new InterruptedException();
}
// Otherwise re-read and check doneness. If we loop then it must have been a spurious
// wakeup
localValue = value;
if (localValue != null & !(localValue instanceof SetFuture)) {
return getDoneValue(localValue);
}
}
}
oldHead = waiters; // re-read and loop.
} while (oldHead != Waiter.TOMBSTONE);
}
// re-read value, if we get here then we must have observed a TOMBSTONE while trying to add a
// waiter.
return getDoneValue(value);
}
示例10: awaitDone
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
/**
* Awaits completion or aborts on interrupt or timeout.
*
* @param timed true if use timed waits
* @param nanos time to wait, if timed
* @return state upon completion
*/
private int awaitDone(boolean timed, long nanos)
throws InterruptedException {
final long deadline = timed ? System.nanoTime() + nanos : 0L;
WaitNode q = null;
boolean queued = false;
for (;;) {
if (Thread.interrupted()) {
removeWaiter(q);
throw new InterruptedException();
}
int s = state;
if (s > COMPLETING) {
if (q != null)
q.thread = null;
return s;
}
else if (s == COMPLETING) // cannot time out yet
Thread.yield();
else if (q == null)
q = new WaitNode();
else if (!queued)
queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
q.next = waiters, q);
else if (timed) {
nanos = deadline - System.nanoTime();
if (nanos <= 0L) {
removeWaiter(q);
return state;
}
LockSupport.parkNanos(this, nanos);
}
else
LockSupport.park(this);
}
}
示例11: block
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
public boolean block() {
while (!isReleasable()) {
if (deadline == 0L)
LockSupport.park(this);
else
LockSupport.parkNanos(this, nanos);
}
return true;
}
示例12: test
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
public static void test(){
System.out.println("unpark startup");
LockSupport.unpark(Thread.currentThread());
// LockSupport.unpark(Thread.currentThread());
System.out.println("park1");
LockSupport.park();
// System.out.println("park2");
// LockSupport.park();
System.out.println("running");
}
示例13: parkCurrentThread
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
void parkCurrentThread() {
LockSupport.park();
}
示例14: parkForFirstConnection
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
void parkForFirstConnection() {
LockSupport.park();
}
示例15: awaitMatch
import java.util.concurrent.locks.LockSupport; //导入方法依赖的package包/类
/**
* Spins/yields/blocks until node s is matched or caller gives up.
*
* @param s the waiting node
* @param pred the predecessor of s, or s itself if it has no
* predecessor, or null if unknown (the null case does not occur
* in any current calls but may in possible future extensions)
* @param e the comparison value for checking match
* @param timed if true, wait only until timeout elapses
* @param nanos timeout in nanosecs, used only if timed is true
* @return matched item, or e if unmatched on interrupt or timeout
*/
private E awaitMatch(Node s, Node pred, E e, boolean timed, long nanos) {
long lastTime = timed ? System.nanoTime() : 0L;
Thread w = Thread.currentThread();
int spins = -1; // initialized after first item and cancel checks
ThreadLocalRandom randomYields = null; // bound if needed
for (;;) {
Object item = s.item;
if (item != e) { // matched
// assert item != s;
s.forgetContents(); // avoid garbage
return LinkedTransferQueue.<E>cast(item);
}
if ((w.isInterrupted() || (timed && nanos <= 0)) &&
s.casItem(e, s)) { // cancel
unsplice(pred, s);
return e;
}
if (spins < 0) { // establish spins at/near front
if ((spins = spinsFor(pred, s.isData)) > 0)
randomYields = ThreadLocalRandom.current();
}
else if (spins > 0) { // spin
--spins;
if (randomYields.nextInt(CHAINED_SPINS) == 0)
Thread.yield(); // occasionally yield
}
else if (s.waiter == null) {
s.waiter = w; // request unpark then recheck
}
else if (timed) {
long now = System.nanoTime();
if ((nanos -= now - lastTime) > 0)
LockSupport.parkNanos(this, nanos);
lastTime = now;
}
else {
LockSupport.park(this);
}
}
}