java.lang.Thread.interrupted()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.7k)|赞(0)|评价(0)|浏览(254)

本文整理了Java中java.lang.Thread.interrupted()方法的一些代码示例,展示了Thread.interrupted()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Thread.interrupted()方法的具体详情如下:
包路径:java.lang.Thread
类名称:Thread
方法名:interrupted

Thread.interrupted介绍

[英]Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it).

A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.
[中]测试当前线程是否已中断。此方法清除线程的中断状态。换句话说,如果连续调用两次此方法,则第二次调用将返回false(除非当前线程在第一次调用清除其中断状态之后,第二次调用检查之前再次中断)。
由于线程在中断时不处于活动状态而被忽略的线程中断将通过返回false的方法反映出来。

代码示例

代码示例来源:origin: google/guava

@Override
 public void tearDown() {
  Thread.interrupted();
 }
});

代码示例来源:origin: google/guava

@Override
 public void tearDown() {
  Thread.interrupted();
 }
});

代码示例来源:origin: google/guava

@Override
public void tearDown() throws Exception {
 super.tearDown();
 // TODO(cpovirk): run tests in other thread instead of messing with main thread interrupt status
 currentThread().interrupted();
 LocalCache.logger.removeHandler(logHandler);
}

代码示例来源:origin: google/guava

@Override
 public Boolean call() throws Exception {
  Object actual;
  if (allowInterruption) {
   actual = future.get();
  } else {
   actual = getUninterruptibly(future);
  }
  assertEquals(RESULT, actual);
  return Thread.interrupted();
 }
});

代码示例来源:origin: ReactiveX/RxJava

@Test
public void interrupted() {
  Iterator<Object> it = Observable.never().blockingLatest().iterator();
  Thread.currentThread().interrupt();
  try {
    it.hasNext();
  } catch (RuntimeException ex) {
    assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
  }
  Thread.interrupted();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void interrupted() {
  Iterator<Object> it = Flowable.never().blockingLatest().iterator();
  Thread.currentThread().interrupt();
  try {
    it.hasNext();
  } catch (RuntimeException ex) {
    assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
  }
  Thread.interrupted();
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
   emitter.onNext(Thread.interrupted());
   emitter.onComplete();
  }
})

代码示例来源:origin: google/guava

@GwtIncompatible // Thread.interrupt
public void testGetUnchecked_interrupted() {
 Thread.currentThread().interrupt();
 try {
  assertEquals("foo", getUnchecked(immediateFuture("foo")));
  assertTrue(Thread.currentThread().isInterrupted());
 } finally {
  Thread.interrupted();
 }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void subscribe(FlowableEmitter<Boolean> emitter) throws Exception {
   emitter.onNext(Thread.interrupted());
   emitter.onComplete();
  }
}, BackpressureStrategy.MISSING)

代码示例来源:origin: google/guava

@GwtIncompatible // threads
public void testSubmitAsync_asyncCallable_returnsInterruptedFuture() throws InterruptedException {
 assertThat(Thread.interrupted()).isFalse();
 SettableFuture<Integer> cancelledFuture = SettableFuture.create();
 cancelledFuture.cancel(true);
 assertThat(Thread.interrupted()).isFalse();
 ListenableFuture<Integer> future =
   submitAsync(constantAsyncCallable(cancelledFuture), directExecutor());
 assertThat(future.isDone()).isTrue();
 assertThat(Thread.interrupted()).isFalse();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void blockingGetErrorInterrupt() {
  final BlockingMultiObserver<Integer> bmo = new BlockingMultiObserver<Integer>();
  Thread.currentThread().interrupt();
  try {
    assertTrue(bmo.blockingGetError() instanceof InterruptedException);
  } finally {
    Thread.interrupted();
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void awaitTerminalEventInterrupt() {
  final TestSubscriber<Integer> ts = TestSubscriber.create();
  ts.onSubscribe(new BooleanSubscription());
  Thread.currentThread().interrupt();
  ts.awaitTerminalEvent();
  assertTrue(Thread.interrupted());
  Thread.currentThread().interrupt();
  ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
  assertTrue(Thread.interrupted());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void awaitTerminalEventInterrupt() {
  final TestObserver<Integer> to = TestObserver.create();
  to.onSubscribe(Disposables.empty());
  Thread.currentThread().interrupt();
  to.awaitTerminalEvent();
  assertTrue(Thread.interrupted());
  Thread.currentThread().interrupt();
  to.awaitTerminalEvent(5, TimeUnit.SECONDS);
  assertTrue(Thread.interrupted());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void blockingGetErrorTimeoutInterrupt() {
  final BlockingMultiObserver<Integer> bmo = new BlockingMultiObserver<Integer>();
  Thread.currentThread().interrupt();
  try {
    bmo.blockingGetError(1, TimeUnit.MINUTES);
    fail("Should have thrown");
  } catch (RuntimeException ex) {
    assertTrue(ex.getCause() instanceof InterruptedException);
  } finally {
    Thread.interrupted();
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void blockingGetDefaultInterrupt() {
  final BlockingMultiObserver<Integer> bmo = new BlockingMultiObserver<Integer>();
  Thread.currentThread().interrupt();
  try {
    bmo.blockingGet(0);
    fail("Should have thrown");
  } catch (RuntimeException ex) {
    assertTrue(ex.getCause() instanceof InterruptedException);
  } finally {
    Thread.interrupted();
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void interrupt() {
  TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0L);
  Thread.currentThread().interrupt();
  try {
    Flowable.just(1)
    .blockingSubscribe(ts);
    ts.assertFailure(InterruptedException.class);
  } finally {
    Thread.interrupted(); // clear interrupted status just in case
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void interrupted() {
  CountDownLatch cdl = new CountDownLatch(1);
  Disposable d = Disposables.empty();
  Thread.currentThread().interrupt();
  try {
    BlockingHelper.awaitForComplete(cdl, d);
  } catch (IllegalStateException ex) {
    // expected
  }
  assertTrue(d.isDisposed());
  assertTrue(Thread.interrupted());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void scheduleDirectInterrupt() {
  Thread.currentThread().interrupt();
  final int[] calls = { 0 };
  assertSame(EmptyDisposable.INSTANCE, Schedulers.trampoline().scheduleDirect(new Runnable() {
    @Override
    public void run() {
      calls[0]++;
    }
  }, 1, TimeUnit.SECONDS));
  assertTrue(Thread.interrupted());
  assertEquals(0, calls[0]);
}

代码示例来源:origin: google/guava

public final void run() {
  try {
   realRun();
   threadShouldThrow("InterruptedException");
  } catch (InterruptedException success) {
   threadAssertFalse(Thread.interrupted());
  } catch (Throwable t) {
   threadUnexpectedException(t);
  }
 }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void timeoutIndicated() throws InterruptedException {
  Thread.interrupted(); // clear flag
  TestSubscriber<Object> ts = Flowable.never()
  .test();
  assertFalse(ts.await(1, TimeUnit.MILLISECONDS));
  try {
    ts.assertResult(1);
    throw new RuntimeException("Should have thrown!");
  } catch (AssertionError ex) {
    assertTrue(ex.toString(), ex.toString().contains("timeout!"));
  }
}

相关文章