JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label Runnable Interface. Show all posts
Showing posts with label Runnable Interface. Show all posts

Implement Runnable vs Extend Thread in Java

10:43 PM
Implement Runnable vs Extend Thread in Java As discussed in Java multi-threading article we can define a thread in the following two ways: By extending Thread class By implementing Runnable interface In the first approach, Our class always extends Thread class. There is no chance of extending any other class. Hence we are missing Inheritance benefits. In the second approach, while implementing Runnable interface we can extends any other class. Hence we are able to use the benefits of Inheritance. Because of the above reasons, implementing Runnable interface approach is recommended than extending Thread class. The significant differences between extending Thread class and implementing the Runnable interface: When we extend Thread class, we can’t extend any other class even we require and When we implement Runnable, we can save a space for our class to extend any other class in future or now. When we extend Thread class, each of our thread creates unique object and associat...

Thread Class vs Runnable Interface

10:40 PM
Thread Class vs Runnable Interface 1. If we extend the Thread class, our class cannot extend any other class because Java doesn’t support multiple inheritance. But, if we implement the Runnable interface, our class can still extend other base classes. 2. We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface.  

.