We all know two ways to use threads. One is to inherit the Thread class and the other is to implement the runnable interface.
In fact, even if you implement the runnable interface, you still need to construct a Thread class object.
View thread Source codeIn fact, this thread class also implements the runnable interface, and the run () method of this thread class is
If (target! = NULL ){
Target. Run ();
}
The target is the runnable interface implementation class specified by the init () method to be called by the constructors of the thread class.
Several constructors of the thread class: thread (), thread (runnable target), thread (threadgroup group, runnable target), thread (string name), thread (threadgroup group, string name) thread (runnable target, string name), thread (threadgroup group, runnable target, string name), thread (threadgroup group, runnable target, string name, long stacksize) private void Init (threadgroup g, runnable target, string name, long stacksize) is called ).
Therefore, if you implement the runnable interface and construct a thread object using this interface implementation class, the run () method of the target runnable interface implementation class will be executed, otherwise, if you inherit from the Thread class directly, the target is null, so you can only overwrite the run () method of the thread class.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.