加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 调用Thread.isInterrupted()的性能成本是多少?

发布时间:2020-12-15 00:41:19 所属栏目:Java 来源:网络整理
导读:从 java源代码来看,它看起来像是本机代码.成本大致相当于易失性读取还是需要获取某种类型的锁定? 解决方法 Thread.isInterrupted()是一个非常便宜的函数.有一些更多的间接,但所有电话都足够快.总结一下: It must be possible for Java to emulate Thread.c
从 java源代码来看,它看起来像是本机代码.成本大致相当于易失性读取还是需要获取某种类型的锁定?

解决方法

Thread.isInterrupted()是一个非常便宜的函数.有一些更多的间接,但所有电话都足够快.总结一下:

It must be possible for Java to emulate Thread.currentThread().isInterrupted() by performing the double indirection Thread::current()->_osthread->_interrupted.

Source:

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();

  if (interrupted && clear_interrupted) {
    osthread->set_interrupted(false);
    // consider thread->_SleepEvent->reset() ... optional optimization
  }

  return interrupted;
}

OSThread实现如下:

volatile jint _interrupted;     // Thread.isInterrupted state

// Note:  _interrupted must be jint,so that Java intrinsics can access it.
// The value stored there must be either 0 or 1.  It must be possible
// for Java to emulate Thread.currentThread().isInterrupted() by performing
// the double indirection Thread::current()->_osthread->_interrupted.
....
volatile bool interrupted() const                 { return _interrupted != 0; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读