Wednesday, February 7, 2018

Thread and Runnable: How different are they?

To create a Thread in your process, you have to have your class implement the Runnable interface and be a subclass or a target composite class of a Thread class so  that the start() method of the Thread can be called. That is it must implement the Runnable interface and is able to be executed through the Thread start method.

Since the Thread class implements the Runnable interface, you can make your class to extend the Thread class and call the start method. The Thread class has many other methods other than the run() method. All these will be compiled into your code.

Alternatively, you can have your class implement the Runnable interface directly and pass it to a Thread constructor as an argument to create a new Thread. The Runnable interface only has one run() method. Therefore, your class is simpler and smaller.

No difference of performances has been observed between Threads created in these two ways.

-------------------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment