Can anyone tell me why the Output becomes different on changing the override annotation? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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(); } }

4th Jun 2021, 11:37 AM
Atul [Inactive]
7 Answers
+ 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.
4th Jun 2021, 12:09 PM
Avinesh
Avinesh - avatar
+ 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.
4th Jun 2021, 5:17 PM
Avinesh
Avinesh - avatar
+ 1
It's not depends on @Override annotation. You are using threads so which thread runs first or simultaneously depends on processor availability...
4th Jun 2021, 12:15 PM
Jayakrishna 🇮🇳
+ 1
Avinesh Jayakrishna🇮🇳 So it isn't possible for a coder to judge the working of the processor in this case?
4th Jun 2021, 12:49 PM
Atul [Inactive]
+ 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.
4th Jun 2021, 2:58 PM
Jayakrishna 🇮🇳
0
Ok thank you Avinesh and Jayakrishna🇮🇳
4th Jun 2021, 5:39 PM
Atul [Inactive]
0
You're welcome.. Atul
4th Jun 2021, 5:55 PM
Jayakrishna 🇮🇳