- Extending the Thread class
- Using Runnable interface(this one prefer)
One way to create a thread in Java is by defining a class that extends the built-in
Thread class. An object of this derived class represents a thread.The next question is: once we extend the Thread class, where do we write the code that the thread will execute?
The Thread class provides a method called run(), which must be overridden to define the task that the thread should perform. When the thread starts, the code inside the run() method is executed.
Suppose we have a class named Derived that extends the Thread class. It would look like this:
Example of creating a thread by Extending from the Thread class:
class ThreadDemo { public static void main(String[] args) { MyThread mt = new MyThread(); mt.start(); for(int i=0; i<50; i++) { System.out.println("i="+i+",i*i="+i*i); } } } class MyThread extends Thread { public void run() { for(int count = 1,row = 1;row<20; row++,count++) { for(int i =0; i<count; i++) { System.out.print("*"); System.out.print("\n"); } } } }
Java does not support multiple inheritance, which means a class cannot extend more than one class at the same time. Therefore, there may be situations where you want to create a thread while also extending another class that is not the Thread class.
To address this limitation, Java provides an alternative way to create threads through the Runnable interface. By implementing the Runnable interface, a class can define the task to be executed by a thread without extending the Thread class.
The Runnable interface contains only one method that must be implemented:
public void run();
If you create a class that implements the Runnable interface, it cannot run as a thread by itself. Instead, you must create an instance of the Thread class and pass the Runnable object to its constructor. The Thread object is then responsible for executing the run() method of the Runnable instance.
The concept becomes clearer with an example. Suppose we want to create a thread by implementing the Runnable interface.
The code would look something like the following:
class ThreadDemo { public static void main(String[] args) { MyThread mt = new MyThread(); Thread t = new Thread(mt); t.start(); } } class MyThread implements Runnable { public void run() { System.out.println("Implementing Runnable interface"); } }
You have now seen that a thread can be created in Java either by extending the
Thread class or by implementing the Runnable interface. As you may have noticed, in both approaches, you must provide an implementation for the run() method.Another common interview question is:
What if you need a thread to return a value after execution? Since the run() method has a void return type, how can this situation be handled? Is there any other interface available for this purpose?
The answer is the Callable interface. Unlike the Runnable interface, the Callable interface allows a task to return a result and throw checked exceptions. It is commonly used along with the Future interface to retrieve the result of an asynchronous computation.
Callable interface, please refer to the next section. Click Here..
No comments:
Post a Comment