More and more languages are providing support for asynchronous programming. This course will show you how to use the asynchronous programming features of Kotlin allowing you to create applications that are more responsive.
As a developer, you want to build fast-running applications without the overhead that threads can cause. In this course, Kotlin: Using Coroutines, you'll learn how coroutines provide a way to use ‘lightweight’ threads within a process that let us take advantage of multi-core processors without much of the overhead. First, you'll learn how to create coroutines. Next, you'll explore how to use them to return data. Finally, you'll discover how to use coroutines in environments such as JavaFX and Android, where thread affinity is really an issue. By the end of this course, you'll have a thorough knowledge of how to use coroutines in Kotlin to create fast and efficient applications.
Course Overview (Music playing) Hi everyone, my name is Kevin Jones, and welcome to my course on Using Coroutines in Kotlin. I'm a founder and developer at Rock Solid Knowledge, a software development company based in the United Kingdom. Asynchronous programming is a technique that lets you scale applications without overloading the operating system by creating too many threads. Kotlin provides excellent support for asynchronous programming through its use of coroutines. Some of the major topics that we will cover include understanding how to create coroutines with various builders, such as launch and runBlocking, using async and await to return data from coroutines, creating channels to allow coroutines to communicate easily, and using actors to manage state in an asynchronous environment. By the end of this course, you'll have an in-depth understanding of how to do asynchronous programming in Kotlin using coroutines. Before beginning the course, you should be familiar with programming Kotlin. I hope you'll join me on this journey to learn coroutines with Kotlin: Using Coroutines course at Pluralsight.
Defining Functions and Running them with Coroutine Builders Hi, welcome back to the Using Coroutines in Kotlin class. I'm Kevin Jones. And in this chapter, we'll actually define and run coroutines using coroutines builders and suspend functions. So the first thing we have to understand is how coroutines are started and run. Coroutines have to be run inside a context. This context sets up the code to execute the coroutine in a non-blocking fashion. And the simplest way to do this is to use a coroutines builder, and that's what we'll do in this chapter. We'll see that in many coroutines builders, such as launch and runBlocking and others as well, and we'll use a few of those in the chapter. We also find that certain functions can only be called inside a coroutine. So certain functions want to interact with the coroutines context. For example, the function we saw previously, delay. So, delay causes the coroutine to suspend itself in a non-blocking fashion. The delay function only makes sense to be run as part of a coroutine. So we need a way of letting the compiler and other code know that these functions are allowed to run inside a coroutine. To do that, we mark functions with the keyword called suspend. And in this chapter, we'll take a look at how we write those functions and how we use the suspend keyword. So, coroutines need to be managed, they need to run inside a context, and to do this, we need to be able to build a context. And this is the job of a coroutine builder. And as we've said, there are coroutine builders such as launch that we've seen, there's runBlocking, and there are others as well. So let's take a look at how we run coroutines and how we use these coroutine builders.