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

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

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

Thread.interrupt介绍

[英]Interrupts this thread.

Unless the current thread is interrupting itself, which is always permitted, the #checkAccess() method of this thread is invoked, which may cause a SecurityException to be thrown.

If this thread is blocked in an invocation of the Object#wait(), Object#wait(long), or Object#wait(long,int) methods of the Objectclass, or of the #join(), #join(long), #join(long,int), #sleep(long), or #sleep(long,int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an java.nio.channels.InterruptibleChannel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a java.nio.channels.ClosedByInterruptException.

If this thread is blocked in a java.nio.channels.Selectorthen the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's java.nio.channels.Selector#wakeup method were invoked.

If none of the previous conditions hold then this thread's interrupt status will be set.

Interrupting a thread that is not alive need not have any effect.
[中]

代码示例

代码示例来源:origin: iluwatar/java-design-patterns

@Override
public void run() {
 writeLock.lock();
 try {
  write();
 } catch (InterruptedException e) {
  LOGGER.info("InterruptedException when writing", e);
  Thread.currentThread().interrupt();
 } finally {
  writeLock.unlock();
 }
}

代码示例来源: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: iluwatar/java-design-patterns

@Override
public void run() {
 readLock.lock();
 try {
  read();
 } catch (InterruptedException e) {
  LOGGER.info("InterruptedException when reading", e);
  Thread.currentThread().interrupt();
 } finally {
  readLock.unlock();
 }
}

代码示例来源: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

@Override
  public void run() {
    t0.interrupt();
  }
}, 200, TimeUnit.MILLISECONDS);

代码示例来源: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

@Override
  public void run() {
    t0.interrupt();
  }
}, 200, TimeUnit.MILLISECONDS);

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

@Override
  public void run() {
    t0.interrupt();
  }
}, 200, TimeUnit.MILLISECONDS);

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

@Test(expected = InterruptedException.class)
public void getInterrupted() throws Exception {
  Thread.currentThread().interrupt();
  fo.get();
}

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

@Test
public void interruptTestWaitStrategy() {
  try {
    Thread.currentThread().interrupt();
    TestWaitStrategy.SLEEP_1000MS.run();
  } catch (RuntimeException ex) {
    assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
  }
}

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

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

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

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

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

@Override
  public void accept(Emitter<Event> s) {
    s.onNext(randomEvent(type, numInstances));
    try {
      // slow it down somewhat
      Thread.sleep(50);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      s.onError(e);
    }
  }
}

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

@Override
  public void accept(Emitter<Event> s) {
    s.onNext(randomEvent(type, numInstances));
    try {
      // slow it down somewhat
      Thread.sleep(50);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      s.onError(e);
    }
  }
}

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

@Test
public void interruptWait() {
  BlockingObservableIterator<Integer> it = new BlockingObservableIterator<Integer>(128);
  try {
    Thread.currentThread().interrupt();
    it.hasNext();
  } catch (RuntimeException ex) {
    assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
  }
}

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

@Test
public void awaitDoneTimed() {
  TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
  Thread.currentThread().interrupt();
  try {
    ts.awaitDone(5, TimeUnit.SECONDS);
  } catch (RuntimeException ex) {
    assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
  }
}

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

@Test
public void awaitDoneTimed() {
  TestObserver<Integer> to = new TestObserver<Integer>();
  Thread.currentThread().interrupt();
  try {
    to.awaitDone(5, TimeUnit.SECONDS);
  } catch (RuntimeException ex) {
    assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
  }
}

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

@Test(timeout = 5000)
public void blockingFirstTimeout() {
  BlockingFirstSubscriber<Integer> bf = new BlockingFirstSubscriber<Integer>();
  Thread.currentThread().interrupt();
  try {
    bf.blockingGet();
    fail("Should have thrown!");
  } catch (RuntimeException ex) {
    assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
  }
}

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

@Test
public void interrupt() {
  TestObserver<Object> to = new TestObserver<Object>();
  Thread.currentThread().interrupt();
  Observable.never().blockingSubscribe(to);
}

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

@Test(timeout = 5000)
public void blockingFirstTimeout2() {
  BlockingFirstSubscriber<Integer> bf = new BlockingFirstSubscriber<Integer>();
  bf.onSubscribe(new BooleanSubscription());
  Thread.currentThread().interrupt();
  try {
    bf.blockingGet();
    fail("Should have thrown!");
  } catch (RuntimeException ex) {
    assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
  }
}

相关文章