Roy
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: Android --- # Kotlin Corourtine in Android ## Coroutine 什麼是Coroutine? 先來暸解這個字是怎麼來的。其實它是由兩個字所組合而來,分別是cooperation(合作)以及routine(例行作業)這兩個字組合起來,其實就可以大致上理解它是合作的例行作業?吧,其實我個人倒是覺得講合作我能理解,至於例行作業我還在努力參透其中的意義,說不定某天會突然開竅(疑?) ## 輕量級Thread? 可以在網路上找到很多的說明,說它是輕量級的Thread。它能做的事情跟Thread一樣,能夠進行異步處理耗時的工作,但是我認為它其實不算是Thread。與其說是一個輕量級的Thread,不如說它是自動協助開發者控制Thread的物件,透過有效的控制達到消耗資源最少卻又同時達到目的的效果。 ![](https://i.imgur.com/Uo5QDvc.png) 參考上圖,我們先來點Android異步操作的基本慨念。基本上我們都知道在android中,執行耗時的工作在UI Thread(Main Thread)中處理是被禁止的,需要處理耗時的工作就必須透過IO Thread,就如同上圖一樣。而在Coroution的操作中就帶很強烈的帶入了這樣的操作概念,可以透過區塊宣告的方式說明現在我想要執行在IO Thread中,什麼時候我想要切回UI Thread中,在實際撰寫與閱讀上就可以很直覺的知道,這點我個人是覺得非常棒。 ## Coroutine 起手式 ### Dependencies 先在Gradle中新增相關的dependencies。 ``` =android //kotlin coroutines implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9" ``` 在使用上我個人覺得真的很簡便,概念上就像是宣告一個程式碼區塊是指定給coroutine執行專用的,就如同下方: ```=android fun coroutineDemo() { CoroutineScope(Dispatchers.IO).launch { } } ``` CoroutineScope代表協程運作的區塊,可以作用在IO或是Main Thread中,視需要自行設定。CoroutineScope內有一個CoroutineContext,這個指的是運作情境,可以透過Dispatchers指定,如上面程式碼所示。除了CoroutineScope還有一個常見的就是GlobalScope,這個Scope繼承CoroutineScope,他的生命週期跟隨者Appication,所以不會被隨意取消掉,一般建議使用上用CoroutineScope就可以了。 以下,我建立幾個方法用來模擬耗時的工作: ```=android fun sleep1000(): String { Thread.sleep(1000) return "Sleep1000" } fun sleep2000(): String { Thread.sleep(2000) return "Sleep2000" } fun sleep3000(): String { Thread.sleep(3000) return "Sleep3000" } ``` 接著我們在scope中透過async這個方法來執行其中一個耗時工作: ```=android=1 CoroutineScope(Dispatchers.IO).launch { var deferred: Deferred<String> = async { sleep1000() } withContext(Dispatchers.Main) { textView.text = deferred.await() } } ``` 說明一下Coroutine裡面幾個關鍵字: - **runblock**: 啟動一個協程,並且block目前的Thread等待此協程執行完畢。 - **launch**: 在不阻塞目前thread的情況下,啟動協程並return一個Job物件。(該Job物件可以用來控制協程。) - **wichContext**: 指定協程要運作的情境(Main or IO Thread)。 - **async**: 在不阻塞目前thread的情況下,啟動協程並且return一個Deferred物件,需要配合await一起使用。 所以根據上面的程式碼,可以這樣解讀: 第1行 = 宣告一個coroutine的執行環境,並且要在IO Thread中執行。 第2行 = 啟動協程,執行sleep1000方法,並且我要回傳一個Defferrd物件。 第3行 = 宣告一個執行在Main Thread的區塊。 第4行 = 宣告textView.text獲得deferrd待sleep1000執行完所得到的結果。 是不是這樣看起來程式碼相當直觀?可以很線性的筆直閱讀下來。在這裡的async會在執行的時候直接先回傳一個deferred,並且該deferred物件會等待執行的結果取得後,再接著往下執行程式碼。 ## 多個協程 如果同時執行多個協程可以嗎?當然可以。在scope中可以啟動多個斜程來執行耗時的工作。可以向下方這樣的寫法: ```=android= CoroutineScope(Dispatchers.IO).launch { var deferred: Deferred<String> = async { sleep1000() } var deferred1: Deferred<String> = async { sleep2000() } var deferred2: Deferred<String> = async { sleep3000() } withContext(Dispatchers.Main) { textView.text = deferred.await()+deferred1.await()+deferred2.await() } } ``` 執行結果如下: ![](https://i.imgur.com/4eKfpez.png =300x500) 根據上面程式碼,在第6行可以把所有結果串起來,待最一個拿到回傳結果的deferred完成後,程式碼接著往下執行。 另外也可以把deferred寫在其他的協程裡面,讓協程等待deferred完成後再接著往下執行。 ```=android=1 CoroutineScope(Dispatchers.IO).async { var deferred: Deferred<String> = async { sleep1000() } var deferred1: Deferred<String> = async { deferred.await()+sleep2000() } var deferred2: Deferred<String> = async { deferred1.await()+sleep3000() } withContext(Dispatchers.Main) { textView.text = deferred2.await() } } ``` 執行果如下: ![](https://i.imgur.com/RMrgOBm.png =300x500) 可以從上面的結果知道這樣總共需要執行6秒的時間才會完成所有的操作。 ## corourtine的操控 上面介紹了coroutine的一些很基本的用法,但是如果今天我想要對某種情境下中斷停止協程的話因該怎麼做呢?上面有說到launch這個方法會回傳一個Job物件,要操控協程,就得靠它了。請參考下面展示的一段程式碼: ```=android=1 private lateinit var countTimeJob : Job ... ``` ```=android=20 fun createCountTimeCoroutineScope() { countOver = false countTimeJob = CoroutineScope(Dispatchers.IO).launch { var i = 0; while (true) { if(countOver){ cancel() //可以在內部取消 } delay(1000) i++ withContext(Dispatchers.Main){ tvCountTime.text = i.toString() } } } } fun cancelCoroutine(){ countTimeJob.cancel() //可以在外部取消 } ``` 當然除了cancel這個取消的方法以外,還有以下的方法可以調用: - **join()**: 等待現在正在執行的job完成後,接著執行。 - **cancalAndJoin()**: 其實就是取消正在執行的協程,重新執行協程。 - **isActive()**: 確認協程是否在執行中。 以上就是coroutine一些簡單的基本用法。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully