Search code examples
javamultithreadingjava-threads

What will happen use run() instead of start() of a thread?


Fallowing thread class is working fine. I can understand its process. Then I changed

mc.srart() into mc.run() but nothing changed and there was no any errors.

Can someone please explain this to me ? can we always use run() instead of start() ?

public class Main {

    public static void main(String[] args) {

        Myclass mc = new Myclass();
        mc.start();
    }
}

class Myclass extends Thread {
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.print(i + "--");
        }
    }
}

Solution

  • Calling run directly on a Thread object defeats the point of having the Thread in the first place.

    If you call run, then run will execute in the current Thread, as a normal method. You must call the startmethod on the Thread to have run execute in a different Thread.

    Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.