What if we call run() directly?

if you see code that calls the run() method directly, that’s perfectly legal. But it doesn’t mean the run() method will run in a separate thread. Calling a run() method directly just means
you’re invoking a method from whatever thread is currently executing, and the run()
method goes onto the current call stack rather than at the beginning of a new call stack.

Example:
class demo extends Thread
{
public void run(){System.out.println("this is run");}
public static void main(String[] args){
demo d=new demo();
d.run();// Legal, but does not start a new thread
}};

No comments:

Post a Comment