0

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 + "--");
        }
    }
}
DSM
  • 31
  • 1
  • 8
  • `t.start()` is the method that the library provides for your code to call in order to start a new thread. `run()` is the method that your code provides for the library to call _in_ the new thread. The `run()` method is the method that defines what the thread will do. – Solomon Slow Jan 26 '15 at 18:50

1 Answers1

5

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357