博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
源码之Thread.interrupted()
阅读量:2155 次
发布时间:2019-05-01

本文共 1735 字,大约阅读时间需要 5 分钟。

Thread.interrupted()

调用t.interrupt()后,第一次返回true, 第2次及之后都返回false

jdk源码

其实都是空方法

interrupted

public static boolean interrupted() {
return currentThread().isInterrupted(true); // ClearInterrupted}

isInterrupted

private native boolean isInterrupted(boolean ClearInterrupted);

c++源码

JVM_IsInterrupted

JVM_QUICK_ENTRY(jboolean, JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted))  JVMWrapper("JVM_IsInterrupted");  // Ensure that the C++ Thread and OSThread structures aren't freed before we operate  oop java_thread = JNIHandles::resolve_non_null(jthread);  MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);  // We need to re-resolve the java_thread, since a GC might have happened during the  // acquire of the lock  JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));  if (thr == NULL) {
return JNI_FALSE; } else {
return (jboolean) Thread::is_interrupted(thr, clear_interrupted != 0); }JVM_END

osThread.hpp

volatile jint _interrupted;     // Thread.isInterrupted statevoid set_interrupted(bool z)   {
_interrupted = z ? 1 : 0; }volatile bool interrupted() const {
return _interrupted != 0; }

os_linux.cpp

bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
assert(Thread::current() == thread || Threads_lock->owned_by_self(), "possibility of dangling Thread pointer"); OSThread* osthread = thread->osthread(); bool interrupted = osthread->interrupted(); // 返回interrupted状态 if (interrupted && clear_interrupted) {
// 现在是中断状态 并且clear_interrupted=true 才会进来 osthread->set_interrupted(false); //设置false // consider thread->_SleepEvent->reset() ... optional optimization } return interrupted;}

转载地址:http://ruawb.baihongyu.com/

你可能感兴趣的文章
【LEETCODE】223-Rectangle Area
查看>>
【LEETCODE】12-Integer to Roman
查看>>
【学习方法】如何分析源代码
查看>>
【LEETCODE】61- Rotate List [Python]
查看>>
【LEETCODE】143- Reorder List [Python]
查看>>
【LEETCODE】82- Remove Duplicates from Sorted List II [Python]
查看>>
【LEETCODE】86- Partition List [Python]
查看>>
【LEETCODE】147- Insertion Sort List [Python]
查看>>
【算法】- 动态规划的编织艺术
查看>>
用 TensorFlow 让你的机器人唱首原创给你听
查看>>
对比学习用 Keras 搭建 CNN RNN 等常用神经网络
查看>>
深度学习的主要应用举例
查看>>
word2vec 模型思想和代码实现
查看>>
怎样做情感分析
查看>>
用深度神经网络处理NER命名实体识别问题
查看>>
用 RNN 训练语言模型生成文本
查看>>
RNN与机器翻译
查看>>
用 Recursive Neural Networks 得到分析树
查看>>
RNN的高级应用
查看>>
TensorFlow-7-TensorBoard Embedding可视化
查看>>