Home » Core Java » Thread Creation

Thread Creation

 

In java language thread means either an instance of class java.lang.Thread or a thread of execution.So in java.lang package there exists a Thread class  which implements Runnable interface,using which we can create Thread.You can just visit the below API codes once to know the internals of Thread class and Runnable interface.

Runnable.java

Thread.java

Ways to create Thread:

1 : By extending Thread class
2 : By implementing Runnable interface

Thread creation By extending Thread class

Thread creation By implementing Runnable Interface


Extends Thread vs Implement Runnable ::

1.If you extend Thread class ,you can not extend any other class ,as multiple inheritance is not supported in Java.
Therefore developers prefer implementing Runnable over extending Thread while creating a thread.Implementing Runnable interface gives developer’s opportunity to extend other class and implementing other interfaces.

2. Executors of java.util.Concurrent package accept Runnable as Task and their  worker threads execute those task.This is another reason for choosing implementing Runnable over extending Thread class.


Leave a comment

Your email address will not be published. Required fields are marked *

eleven + nineteen =