45
loading...
This website collects cookies to deliver better user experience
Make Sure
That You Have Included Coroutines Dependency If It's Not Included:implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
Returns The Work Which We Have Assigned
. Ex:- Loading Data, Network Operations, Working With UI, Some Background Stuff Etc., val work = GlobalScope.launch(Dispatchers.Main) {
Log.d("jobs-.join()", "Started Execution From GlobalScope Which Is In A Variable")
}
Jobs
May Help You.Jobs
Is Not A Function. We Have Multiple Suspend Functions And Other CodeBlocks Which Can Make Our Job Easy😉:.join()
.cancel()
Let's Go In-Deep To Check What Actually These Functions Do And How Can Be Used And When These Can Be Used.
I'm Going To Use runBlocking() Which I Have Discussed In The Previous Blog, So That We'll Get An Idea How & When These Functions Can Be Used.
runBlocking() Blocks Whole Thread Until The Execution Of runBlocking() Completes.
runBlocking()
, You Can Execute That Variable In A Coroutine As Well:As Name Suggests, Our Job||Work Can Join Some other Suspend Function Or Coroutine.
.join()
Or Other Jobs In runBlocking() The Execution Of Variable Completes First And Then Back To The Normal State Of Our Code..join()
Or Other Jobs To A Variable Where We have Stored Our Coroutine Execution Will Primarily Completes First
And Then Remaining Execution Of Other Code Block Will Be Continued. [If You Are Working With runBlocking()
]work
To Demonstrate:val work = GlobalScope.launch(Dispatchers.Main) {
Log.d("jobs-.join()", "Started Execution From GlobalScope Which Is In A Variable")
}
GlobalScope.launch(Dispatchers.Default) {
delay(2000L)
Log.d("jobs-.join()", "Running From ${Thread.currentThread().name}")
}
GlobalScope.launch(Dispatchers.IO) {
delay(2000L)
Log.d("jobs-.join()", "Running From IO Coroutine")
}
.join()
Is Executed In runBlocking()
:
val work = GlobalScope.launch(Dispatchers.Main) {
Log.d("jobs-.join()", "Started Execution From GlobalScope Which Is In A Variable")
}
GlobalScope.launch(Dispatchers.Main) {
delay(2000L)
Log.d("jobs-.join()", "Running From ${Thread.currentThread().name}")
textView.text = "runBlocking() Execution Has Completed"
}
GlobalScope.launch(Dispatchers.IO) {
delay(2000L)
Log.d("jobs-.join()", "Running From IO Coroutine")
}
runBlocking {
Log.d("jobs-.join()", "Executing From runBlocking()")
delay(3000L)
work.join()
Log.d("jobs-.join()", "Executing Has Completed")
}
work.join()
In runBlocking() As .join() Is A Suspend Function..join()
Is Executed In Coroutine
:
val work = GlobalScope.launch(Dispatchers.Default) {
Log.d("jobs-.join()", "Started Execution From GlobalScope Which Is In A Variable")
}
GlobalScope.launch(Dispatchers.Main) {
delay(2000L)
Log.d("jobs-.join()", "Running From ${Thread.currentThread().name}")
textView.text = "Coroutine Execution Has Completed"
}
GlobalScope.launch(Dispatchers.IO) {
Log.d("jobs-.join()", "Running From IO Coroutine")
work.join()
delay(2000L)
}
work.join()
In Coroutine As .join() Is A Suspend Function.work
Variable And Then Other Coroutine Started Their Respected Work/Job As Well:.join()
Function Is Executed First..join()
Which Can Be Joined In Other Coroutine To Work Simultaneously With That Coroutine || In runBlocking() To Execute That Variable Primarily.As Name Suggests, Our Job||Work Can Be Cancelled.
repeat(...){...}
Block Where I Have Added Log
Statements By 1 Second Delay Interval, By 5 Times.val work = GlobalScope.launch(Dispatchers.Default) {
repeat(5) {
Log.d("jobs-.join()", "Started Execution From GlobalScope Which Is In A Variable")
delay(1000L)
}
}
.cancel()
Is Executed In runBlocking()
:.cancel()
Will Cancel Our Work Immediately, So I am Delaying For 2 Seconds After That Our Work || Job Will Be Cancelled.That Mean repeat's Block Code Will Be Executed 2 Times And Then The Whole Work Will Be Cancelled.
val work = GlobalScope.launch(Dispatchers.Default) {
repeat(5) {
Log.d("jobs-.cancel()", "Started Execution From GlobalScope Which Is In A Variable")
delay(1000L)
}
}
runBlocking {
delay(2000L)
work.cancel()
Log.d("jobs-.cancel()", "Work Is Cancelled")
}
work.cancel()
In runBlocking() As .cancel() Is A Suspend Function..cancel()
Is Executed In Coroutine
:.cancel()
Will Cancel Our Work Immediately, So I am Delaying For 2 Seconds After That Our Work || Job Will Be Cancelled.That Mean repeat's Block Code Will Be Executed 2 Times And Then The Whole Work Will Be Cancelled.
val work = GlobalScope.launch(Dispatchers.Default) {
repeat(5) {
Log.d("jobs-.cancel()", "Started Execution From GlobalScope Which Is In A Variable")
delay(1000L)
}
}
GlobalScope.launch(Dispatchers.Default) {
delay(2000L)
work.cancel()
Log.d("jobs-.cancel()", "Work Is Cancelled")
}
work.cancel()
In Coroutine As .cancel() Is A Suspend Function.However Cancelling A Coroutine Is Not Easy Always As I have Demonstrated And Cancelled Above, In Few Cases It May Just Crash The Whole Code As Well If You Didn't Properly Handle It. By The Way Most Of The Time, You May Execute **withTimeOut(...){...}
Rather Than Cancelling The Coroutine Which Makes Things More Simple Though.**
.cancel()
Which Can Cancel The Execution (If You Want)..cancel()
We Can Cancel The Execution But The Job || Work Which We Have Executed To A Variable Won't Know That Job || Work Has Cancelled, To Over Come That isActive()
Can Be Used With if
Statement As I Have Demonstrated Below:val work = GlobalScope.launch(Dispatchers.Default) {
if (isActive) {
repeat(5) {
Log.d(
"jobs-isActive",
"Coroutine Is Still Active"
)
delay(1000L)
}
}
}
runBlocking {
delay(2000L)
work.cancel()
Log.d("jobs-isActive","Coroutine Has Cancelled")
}
work.join
But The Code Is Executing As I Have Added It, How's That Possible🤔?isActive
In The Code, The Variable Continues To Work Until That Particular Variable Is Cancelled; As It Didn't Get Cancelled That Mean It's Still Active To Execute. That's The Reason Even Though I Didn't Mention work.join
, Coroutine Will Work Like Charm ⚡. But Once That Variable Gets Cancelled The Variable Will No Longer Be Active.Yes
It's Possible With withTimeOut(...){...}
In Which You'll Give A Specific Time If That Job || Work Didn't Get Completed Within That Particular Time It Will Be Cancelled Automatically Without Any runBlocking() And Other Stuff:
GlobalScope.launch(Dispatchers.Default) {
withTimeout(2000L) {
repeat(5) {
Log.d("jobs-withTimeOut", "Coroutine Is Still Active")
delay(1000L)
}
}
}
GlobalScope.launch(Dispatchers.Default) {
withTimeout(2000L) {
if (isActive) {
repeat(5) {
Log.d("jobs-withTimeOut", "Coroutine Is Still Active")
delay(1000L)
}
}
}
}
Well That's All For Now🙌
Bye🤗