解决方法
Thread.isInterrupted()是一个非常便宜的函数.有一些更多的间接,但所有电话都足够快.总结一下:
It must be possible for Java to emulate
Thread.currentThread().isInterrupted()
by performing the double indirectionThread::current()->_osthread->_interrupted
.
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; }