0

Can anybody please please explain the code with comments in java

https://code.sololearn.com/c9zUz5X45G0z/?ref=app

14th Mar 2021, 12:08 PM
Sufia Hashmi
Sufia Hashmi - avatar
20 Respostas
+ 5
The code is about multi threading. It means you are running 2 different piece of code at the same time simultaneously but one of them is waiting for 0.5 seconds after each execution new MainThread(); //starting parallel thread, try { for (int i=1;i<=5;i++) { System.out.println("mTh"+" "+i); Thread.sleep(500); //waiting }
14th Mar 2021, 1:17 PM
deleted
+ 2
AssasinStudent could u describe in detail
14th Mar 2021, 1:18 PM
Sufia Hashmi
Sufia Hashmi - avatar
+ 2
AssasinStudent thank youuuuuu soo much dear
14th Mar 2021, 1:20 PM
Sufia Hashmi
Sufia Hashmi - avatar
+ 2
Normaly programs run step by step for example print(2) can't be executed before print(1). print(1) print(2) print(3) But especially for user interface applications like android apps. you want some peace of code to print user interface, like keeping software alive. and some other piece of code to do something like an calculation. to do this you have to run this codes simultaneously. so you can print 1,2,3 in mixing order. If you pause one of them using Thread.sleep() for longer time. other two will be executed more. to define this you haveto create a class like this ----------------- class SomeName implements Runnable { } inside SomeName class you define a thread and start it like --------------- Thread task1 = new Thread(); in your examples it is defined inside constructor but you can define it inside any other method. there is other methods for stoping thread or restarting it. This is important and not hard topic. you can search google for multithrading in java there is a lot of examples. don't be confused with try catch block and exceptions just concentrate on core parts in your code there is 2 piece of code code 1 in MainThrad: for (int i=1;i<=5;i++) { System.out.println("ch"+" "+i); Thread.sleep(500); //WAIT 500 miliseconds or 0.5 seconds } code 2 in public static void main for (int i=1;i<=5;i++) { System.out.println("mTh"+" "+i); Thread.sleep(500); //WAIT 500 milliseconds } When you look at the output you will see this: ch 1 //CODE 1 EXECUTED mTh 1 // CODE 2 EXECUTED ch 2 // CODE 1 .. mTh 2 //CODE 2 .. ch 3 mTh 3 ch 4 mTh 4
14th Mar 2021, 2:04 PM
deleted
+ 1
AssasinStudent how muvh tym?
14th Mar 2021, 2:02 PM
Sufia Hashmi
Sufia Hashmi - avatar
+ 1
AssasinStudent thank you
14th Mar 2021, 2:15 PM
Sufia Hashmi
Sufia Hashmi - avatar
+ 1
For example if you want to play 2 different audio file you can use this. otherwise you can not play second one inside your program (at the same time i mean)
14th Mar 2021, 2:31 PM
deleted
+ 1
AssasinStudent in simple 2 run the different code at a time we use "this"
14th Mar 2021, 2:32 PM
Sufia Hashmi
Sufia Hashmi - avatar
+ 1
yes you got it:) This is very important we have to learn soon or later
14th Mar 2021, 2:33 PM
deleted
+ 1
🔥🔥🔥
15th Mar 2021, 1:32 PM
Christoval Reyes
Christoval Reyes - avatar
0
Hi. I have simplified your code a bit. I hope you understand better know https://code.sololearn.com/cA12a3A7a3A1
14th Mar 2021, 2:23 PM
deleted
0
AssasinStudent dear why we sre using "this"
14th Mar 2021, 2:25 PM
Sufia Hashmi
Sufia Hashmi - avatar
0
If you want to run 2 different code simultaneously how you can do this?
14th Mar 2021, 2:26 PM
deleted
0
thank you too. If you have any other thing to ask i am always ready
14th Mar 2021, 2:37 PM
deleted
14th Mar 2021, 2:38 PM
Sufia Hashmi
Sufia Hashmi - avatar
15th Mar 2021, 9:04 PM
Christoval Reyes
Christoval Reyes - avatar
0
Hi
16th Mar 2021, 8:10 AM
Odilbek Usmonjonov
Odilbek Usmonjonov - avatar
0
Hello
16th Mar 2021, 8:56 AM
Abdulaziz Mansurov
Abdulaziz Mansurov - avatar
- 1
Idk
14th Mar 2021, 2:26 PM
Sufia Hashmi
Sufia Hashmi - avatar
- 1
AssasinStudent thanx dear
14th Mar 2021, 2:35 PM
Sufia Hashmi
Sufia Hashmi - avatar