‫Hello my friends.
‫From the previous video we learned that the Coroutines can be executed sequentially and this is the
‫default sequence of the Coroutines.
‫In this video we will learn how to execute the Coroutines in a parallel way.
‫But first I will add a tag here in the same way tag.
‫I will make a message telling the app started.
‫I will launch the program now just to demonstrate.
‫Something.
‫From the previous exercise.
‫Clear.
‫I will stop it.
‫Clear Logcat and then run the application.
‫You can see the application started at 42 seconds.
‫After five seconds, the function one is done and five plus seven, which would be.
‫45.
‫You can see that the application starts at second.
‫Equal to 42 plus five would be 47 plus seven would be 45.
‫And this is good.
‫This is the sequence.
‫The application starts after seven seconds.
‫The function one will returns after five seconds of the function one return.
‫It will be executing function two and returning it.
‫This is good.
‫This is the sequence.
‫Now, if I want to run this application with these two functions in a parallel way.
‫I don't want to wait for the.
‫Do something useful function to be executed and return.
‫And then I need to execute.
‫Do something useful to.
‫I need to start these two functions in parallel.
‫So for that I want to use a keyword keyword called async async.
‫Here will do or will do asynchronous.
‫The execution of the functions and the coroutines.
‫So this is the first async.
‫Do something and here async do something useful.
‫Do I will move them.
‫And here there is a problem because the result could not be added because here using plus job job is
‫an error.
‫The operator plus on two job objects is meaningless because when using the async it will return the
‫object.
‫Job, so I want to use one dot await it will return the integer.
‫And here.
‫2.8.
‫Let's run the program and see if the methods are being executed in parallel way or not.
‫Let me clear the logcat.
‫And then run the application.
‫Here we go.
‫We started the app at.
‫A second equal to 28 after five seconds.
‫The function one is done and after only two seconds from the function, one is done.
‫Function two is done.
‫If you calculate 28 plus seven, you can see the function two.
‫And if you calculate 28 plus five, you can see function one is executed.
‫Now it's not waiting function two or function one to be fully executed and returned to start.
‫Every effort to start the function to.
‫Both functions are started using the same application life cycle.
‫So when the application starts, the function one is starting and the function two is starting.
‫And the proof for this I will make do something useful.
‫Equal to nine seconds and do something useful to equal to seven seconds.
‫And let's see if it returns the function two at first.
‫So we are using the parallel way and here we go.
‫We can see that function two is executed before function one because function one is taking only seven
‫seconds to be executed while function one is taking nine seconds to be executed.
‫So from the time of launching the application here at five zero, it took eight seconds to execute function
‫or seven seconds to execute function two because 50 plus 9.999 and took nine seconds to execute function
‫one.
‫And this is the parallel execution of Coroutines in Kotlin.