+ 1
Can anyone tell me why the Output becomes different on changing the override annotation?
class MyThread1 extends Thread{ // @Override public void run(){ int i =0; while(i<40){ System.out.println("My Cooking Thread is Running"); System.out.println("I am happy!"); i++; } } } class MyThread2 extends Thread{ @Override public void run(){ int i =0; while(i<40){ System.out.println("Thread 2 for Chatting with her"); System.out.println("I am sad!"); i++; } } } public class cwh_70 { public static void main(String[] args) { MyThread1 t1 = new MyThread1(); MyThread2 t2 = new MyThread2(); t1.start(); t2.start(); } }
7 Réponses
+ 3
The @Override is just an annotations and is in no way linked to how your code behaves. It should behave the same with or without it since it is not mandatory.
+ 2
No, a programmer has no control over it. Threads though execute simultaneously but you cannot predict the order of their execution. These are asynchronous in nature.
+ 1
It's not depends on @Override annotation. You are using threads so which thread runs first or simultaneously depends on processor availability...
+ 1
Avinesh Jayakrishna🇮🇳 So it isn't possible for a coder to judge the working of the processor in this case?
+ 1
Threads are used for better utilization of processor. So it is for better performance and time . Order of execution of methods will not guaranteed by coder then.
You can set priority for thread if you want order of execution but it's working is depends on OS support mostly.. Read about threads for more. Hope it helps.
0
Ok thank you Avinesh and Jayakrishna🇮🇳
0
You're welcome.. Atul