0
Can anybody please please explain the code with comments in java
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
}
+ 2
AssasinStudent could u describe in detail
+ 2
AssasinStudent thank youuuuuu soo much dear
+ 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
+ 1
AssasinStudent how muvh tym?
+ 1
AssasinStudent thank you
+ 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)
+ 1
AssasinStudent in simple 2 run the different code at a time we use "this"
+ 1
yes you got it:) This is very important we have to learn soon or later
+ 1
🔥🔥🔥
0
Hi. I have simplified your code a bit. I hope you understand better know
https://code.sololearn.com/cA12a3A7a3A1
0
AssasinStudent dear why we sre using "this"
0
If you want to run 2 different code simultaneously how you can do this?
0
thank you too. If you have any other thing to ask i am always ready
0
0
Hi
0
Hello
- 1
Idk
- 1
AssasinStudent thanx dear