Creating a Java Thread by extending Thread Class

Creating a Java Thread by extending Thread Class
thread can be created in Java programs in two ways – by extending Thread class and by implementingRunnable interface. In case, where the class extends a Tread class, it must override run() method of the Thread class. The program may override the other methods also. If the program uses Runnable interface, it needs to implement only run() method.
The first way to create a thread is to create a subclass of the Thread class. This class must override the run( ) method and it may also override the other methods defined with the class. Then the class that needs the thread can create an object of the class that extends thread class. Following example shows the concept of threads clearly.
Let us now create a sample Fibonacci Program using threads that generates Fibonacci series. This program creates the thread by extending thread class:
 OUTPUT
To crate threads, create a new class that extends the Thread class, and instantiate that class. The extending class must override the run() method and call start() method to begin execution of the thread.

0 comments:

Post a Comment