Single sequential flow of execution is a thread.
--we can create a thread in 2 two ways, one by extending Thread class and the other by implementing the Runnable interface.
--First one
--extend the java.lang.Thread class
--override the run ()
class MyThread extends Thread {
public void run() {
System.out.println("Important job running in MyThread");
}
}
The limitation with this approach (besides being a poor design choice in most cases) is that if you extend Thread, you can't extend anything else. And it's not as if you really need that inherited Thread class behavior, because in order to use a thread you'll need to instantiate one anyway.
Keep in mind that you're free to overload the run() method in your Thread subclass:
class MyThread extends Thread {
public void run() {
System.out.println("Important job running in MyThread");
}
public void run(String s) {
System.out.println("String in run is " + s);
}
}
But know this: The overloaded run(String s) method will be ignored by the Thread class unless you call it yourself. The Thread class expects a run() method with no arguments, and it will execute this method for you in a separate call stack after the thread has been started. With a run(String s) method, the Thread class won't call the method for you, and even if you call the method directly yourself, execution won't happen in a new thread of execution with a separate call stack. It will just happen in the same call stack as the code that you made the call from, just like any other normal method call.