I am newbie to java. One doubt.
I read that, internally, Thread class’s run() method calls the Runnable interface’s run().
My Question is,
How the Thread class’s run() method calls the Runnable interface’s run() ?
thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The Runnable interface contains only one method: the
run()method. TheThreadclass actuallyimplements the
Runnableinterface.Hence, when you inherit from the Thread class, your subclass also implements the Runnable interface.
Here’s an example, how it all happens :
The new Thread object’s
start()method is called to begin execution of the new thread of control.The reason we need to pass the runnable object to the thread object’s constructor is that the thread must have some way to get to the
run()method we want the thread to execute. Since we are no longer overriding therun()method of the Thread class, the defaultrun()method of the Thread class is executed,this default run() method looks like this:
Here,
targetis the runnable object we passed to the thread’s constructor. So the thread begins execution with therun()method of the Thread class, which immediately calls therun()method of our runnable object.