It may be a basic question, i was confused with this,
in one file i have like this :
public class MyThread extends Thread {
@Override
public void run() {
//stuffs
}
}
now in another file i have this :
public class Test {
public static void main(String[] args) {
Thread obj = new MyThread();
//now cases where i got confused
//case 1
obj.start(); //it makes the run() method run
//case 2
obj.run(); //it is also making run() method run
}
}
so in above what is the difference between two cases, is it case 1 is creating a new thread and case 2 is not creating a thread ? that’s my guess…hope for better answer SO guys.Thanks
start()runs the code inrun()in a new thread. Callingrun()directly does not executerun()in a new thread, but rather the threadrun()was called from.If you call
run()directly, you’re not threading. Callingrun()directly will block until whatever code inrun()completes.start()creates a new thread, and since the code inrunis running in that new thread,start()returns immediately. (Well, technically not immediately, but rather after it’s done creating the new thread and kicking it off.)Also, you should be implementing runnable, not extending thread.