Social Media and the Workplace

There are both ethical and legal implications of social media use within the workplace. People can be fired from their jobs for posting information that is problematic. For example, if a healthcare…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Coroutines in Android

What are coroutines?

Coroutines help to manage long-running tasks that might otherwise block the main thread and cause your app to become unresponsive. A coroutine is a concurrency design pattern that you can use on Android to simplify code that executes asynchronously.

As you see in above diagram, in your main thread you can perform only small operations. If you want to perform long running operations in your main thread then your app might be crash and slow. Or if you create no. of background/worker thread then out of memory issue is coming. Coroutines are a special type of function that deliberately yield control over to the caller, but does not end its context in the process, instead maintaining it in an idle state.

Coroutine defines a series of rules and configurations that define how and where a coroutine is going to be executed.

When we use withContext, it will run in series instead of parallel. That is a major difference.

Suspend : As their name suggests, they are functions that suspend the execution of a coroutine at some point, and then resume the execution of the function again when the result is ready.

The launch will not block the main thread, but on the other hand, the execution of the rest part of the code will not wait for the launch result since launch is not a suspend call.

Async is also used to start the coroutines, but it blocks the main thread at the entry point of the await() function in the program.

Add a comment

Related posts:

Dumpster Rental

When it comes to junk removal, many homeowners are overwhelmed at the thought of tackling such a task. However, if you hire a professional junk removal service, you can kick back, relax, and let the…

sacrifice for others

I read the autobiography of edhi sb and took a great lesson .The most high lited thing in his story is that , He spent one paisa to the needy person every day ,which his mother was giving him . this…

A MOTIVATIONAL QUOTE

For every wrong direction you go in the attainment of your goal; the right way eventually shows up sooner or later.. “A MOTIVATIONAL QUOTE” is published by The Writer.