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 + "--");
}
}
}
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 start
method 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.