Java – call thread What is the performance cost of isinterrupted()?
•
Java
From the Java source code, it looks like native code Is the cost roughly equivalent to volatile reads or does it need to acquire some type of lock?
Solution
Thread. Isinterrupted () is a very cheap function There are some more indirect calls, but all the calls are fast enough To sum up:
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; }
The implementation of osthread is as follows:
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; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码