<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arifgur on Medium]]></title>
        <description><![CDATA[Stories by Arifgur on Medium]]></description>
        <link>https://medium.com/@arifgur94?source=rss-57fd43d210b1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ca-eLy0pn7djjwaQ2vEVnQ.jpeg</url>
            <title>Stories by Arifgur on Medium</title>
            <link>https://medium.com/@arifgur94?source=rss-57fd43d210b1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 21:33:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arifgur94/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to Kotlin Coroutines]]></title>
            <link>https://blog.simprasuite.com/introduction-to-kotlin-coroutines-9d90cccb1a81?source=rss-57fd43d210b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d90cccb1a81</guid>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[asynchronous-programming]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[threads]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Arifgur]]></dc:creator>
            <pubDate>Tue, 31 May 2022 11:03:30 GMT</pubDate>
            <atom:updated>2022-05-31T11:03:30.634Z</atom:updated>
            <content:encoded><![CDATA[<h4>A beginner guide for Kotlin Coroutines</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kp9SQy_d6MyfswDVr4NGYA.jpeg" /></figure><p>Kotlin Coroutines are used for long running operations such as reading data from an API or downloading a file on Android . That kind of long running operations need to be done without blocking the UI and the asynchronous programming is a way to do that.</p><p>Asynchronous programming is a technique that enables a program to start a long running task and to continue to other events while the task runs.</p><p>Coroutines is the recommended solution for asynchronous programming on Kotlin. because it is lightweight. You can run many coroutines on a single thread and the other operations does not affected. An example that runs a coroutine 100.000 times is given below and it can be seen that application does not crash and coroutine does not block other events while it prints Android 100.000 times. Let’s create a new Android project and try this in the onCreate method. First of all add dependency to the app level gradle.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/badbf60fa267652d9cabe656e2baf759/href">https://medium.com/media/badbf60fa267652d9cabe656e2baf759/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5eb4b1984696f94ee617e54e6db56bf/href">https://medium.com/media/f5eb4b1984696f94ee617e54e6db56bf/href</a></iframe><p>A brief introduction for Coroutines is given and some techniques in the context of it are explained in the guide. These are <strong>globalScope, runBlocking, coroutineScope, suspend function, async and await, and job</strong>.</p><h4><strong>GLOBAL SCOPE vs RUN BLOCKING</strong></h4><p>GlobalScope is not bound to any job. It is used to launch top level coroutines which are operating on the whole application lifetime and are not cancelled prematurely. An example which shows how to use global scope is below. It can be seen that a line is executed before the global scope and while global scope is waiting for five seconds the program continues to line after global scope. After 5 seconds println function in the coroutine is executed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03dfa2a92ce37a154aa12ee6f66f0a84/href">https://medium.com/media/03dfa2a92ce37a154aa12ee6f66f0a84/href</a></iframe><p>On the other hand run blocking is a bit different from global scope. It blocks the lines after run blocking until coroutine is finished. An example for usage of it is</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e971e562ce2f4ce56ed55c2d7a09f0b/href">https://medium.com/media/7e971e562ce2f4ce56ed55c2d7a09f0b/href</a></iframe><h4><strong>NESTED COROUTINES</strong></h4><p>I explain the coroutine scope too while showing the nested coroutines. Coroutine scope is more common than others because the threat where the coroutine is executed can be selected by Dispatchers. The options for Dispatchers are;</p><p><strong>Dispatcher.Default</strong> is used for CPU intensive processes such as visual processing, sorting alphabetically a very long array.</p><p><strong>Dispatchers.IO</strong> is used to read data from the internet.</p><p><strong>Dispatchers.Main</strong> is for user interface operations.</p><p><strong>Dispatchers.Unconfined</strong> is not confined to any specific thread.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/510857c3f6b51aef427d9167862b1e21/href">https://medium.com/media/510857c3f6b51aef427d9167862b1e21/href</a></iframe><p>A nested coroutine can be seen from below code. While a five second long process is executing another long running process is executed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f75887b876f45bedc757d2b0a081205/href">https://medium.com/media/2f75887b876f45bedc757d2b0a081205/href</a></iframe><h4><strong>SUSPEND FUNCTION</strong></h4><p>Suspend function is a function where a coroutine can be run inside it. It can be suspended, or paused and continued. If a function needs a coroutine scope inside it, it must be suspend function. An example can be seen below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b15625edda8629be994784a567eabf54/href">https://medium.com/media/b15625edda8629be994784a567eabf54/href</a></iframe><h4><strong>ASYNC AND AWAIT</strong></h4><p>If more than one long running operations are executing in a coroutine and their terminating times are different, you can want to wait both of them are finished. For example two long running processes are simulated below code. One of them takes two seconds and the other takes four seconds. In order to use responses of them the program needs to wait both of them is finished. In order to do that async and await pattern is used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73fc8fdccd0052207b6b1e6fe39eb4ca/href">https://medium.com/media/73fc8fdccd0052207b6b1e6fe39eb4ca/href</a></iframe><h4><strong>JOB</strong></h4><p>A coroutines piece of code can be manipulated using Job which is in the context of coroutines. Whenever a new coroutine is launched it will return a job. The functions that are available on the Job interface are <strong>start(), join(), cancel()</strong>. A usage of it can be seen below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/355c32dc521d5fd84d75ce703904b1a0/href">https://medium.com/media/355c32dc521d5fd84d75ce703904b1a0/href</a></iframe><h4>CONCLUSION</h4><p>A brief introduction about Kotlin Coroutines is given in the guide. On Android, coroutines help to manage long-running task that might otherwise block the main threat. Coroutines enable to write cleaner and more concise app code and to make more responsive app.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d90cccb1a81" width="1" height="1" alt=""><hr><p><a href="https://blog.simprasuite.com/introduction-to-kotlin-coroutines-9d90cccb1a81">Introduction to Kotlin Coroutines</a> was originally published in <a href="https://blog.simprasuite.com/">Simpra Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>