Can you override the run() method ?

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);
}}
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.

No comments:

Post a Comment