欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂Android培訓(xùn)之Kotlin學(xué)習(xí)教程之協(xié)程Coroutine

2018-07-17 15:28:56 1566瀏覽

今天扣丁學(xué)堂給大家整理了一下關(guān)于Android培訓(xùn)Kotlin學(xué)習(xí)教程之協(xié)程Coroutine的詳細(xì)介紹,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

首先Coroutine翻譯為協(xié)程,Google翻譯為協(xié)同程序,一般也稱為輕量級(jí)線程,但需要注意的是線程是操作系統(tǒng)里的定義概念,而協(xié)程是程序語言實(shí)現(xiàn)的一套異步處理的方法。


在Kotlin文檔中,Coroutine定義為一個(gè)可被掛起的計(jì)算實(shí)例,下面話不多說了,來一起看看詳細(xì)的介紹吧。

?配置

build.gradle中dependencies添加下面2行,注意coroutine目前仍處于experiment階段,但Kotline官方保證向前兼容。

dependencies{
implementation'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5'
implementation"org.jetbrains.kotlinx:kotlinx-coroutines-android:0.19.3"
}

實(shí)例

我們看一個(gè)簡單Android示例:


  activity_coroutine.xml
  <?xmlversion="1.0"encoding="utf-8"?>
  <android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".coroutine.CoroutineActivity">
  <TextView
  android:id="@+id/tvHello"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
  </android.support.constraint.ConstraintLayout>
  CoroutineActivity.kt
  classCoroutineActivity:AppCompatActivity(){
  overridefunonCreate(savedInstanceState:Bundle?){
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_coroutine)
  setup()
  }
  funsetup(){
  launch(UI){//launchcoroutineinUIcontext
  for(iin10downTo1){//countdownfrom10to1
  tvHello.text="Countdown$i..."http://updatetext
  delay(1000)//waithalfasecond
  }
  tvHello.text="Done!"
  }
  }
  }


運(yùn)行程序tvHello從10倒計(jì)時(shí)顯示到1,最后顯示"Done!"

代碼分析:

我們重點(diǎn)分析setup()函數(shù)

launch(UI){...}-----在UIcontext下啟動(dòng)coroutine

delay(1000)----將當(dāng)前coroutine掛起1秒

看到這里你可能會(huì)疑惑,Android開發(fā)中不是禁止在主線程下做延遲或者阻塞操作嗎?

我們回顧下Coroutine的定義:一個(gè)可被掛起的計(jì)算實(shí)例。

Coroutine不是線程,所以掛起Coroutine不會(huì)影響當(dāng)前線程的運(yùn)行。

取消Coroutine運(yùn)行

我們修改下上面的代碼:

  classCoroutineActivity:AppCompatActivity(){
  lateinitvarjob:Job
  overridefunonCreate(savedInstanceState:Bundle?){
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_coroutine)
  setup()
  }
  funsetup(){
  job=launch(CommonPool){//launchcoroutineinUIcontext
  for(iin10downTo1){//countdownfrom10to1
  tvHello.text="Countdown$i..."http://updatetext
  delay(1000)//waithalfasecond
  }
  tvHello.text="Done!"
  }
  }
  overridefunonPause(){
  super.onPause()
  job.cancel()
  }
  }


重點(diǎn)是launch(UI)返回給一個(gè)job實(shí)例,通過job.cancel()取消coroutine。

Coroutine和thread關(guān)系

我們?cè)俜治鱿?br />
launch(UI)

這行代碼是指將coroutine指派在UI線程上運(yùn)行

當(dāng)我們運(yùn)行一段cpu耗時(shí)操作時(shí),則需要將coroutine指定在非UI線程上。

我們寫成:

launch(){...}

這行代碼等價(jià)于:

launch(CommonPool){...}

我們分析下CommonPool的實(shí)現(xiàn),發(fā)現(xiàn)它會(huì)根據(jù)當(dāng)前cpu的核數(shù)創(chuàng)建一個(gè)線程池提供給Coroutine使用。

  privatefuncreatePlainPool():ExecutorService{
  valthreadId=AtomicInteger()
  returnExecutors.newFixedThreadPool(defaultParallelism()){
  Thread(it,"CommonPool-worker-${threadId.incrementAndGet()}").apply{isDaemon=true}
  }
  }
  privatefundefaultParallelism()=(Runtime.getRuntime().availableProcessors()-1).coerceAtLeast(1)

通過上面的分析,我們理解了Coroutine是一個(gè)運(yùn)行在線程上的可被掛起的計(jì)算單元實(shí)例,對(duì)Coroutine的delay,cancel操作不會(huì)影響線程的運(yùn)行,所以使用Coroutine,可以使我們更加方便得處理異步操作,比如網(wǎng)絡(luò)請(qǐng)求,數(shù)據(jù)存儲(chǔ)等。

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值。

扣丁學(xué)堂微信公眾號(hào)



【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】



查看更多關(guān)于“Android開發(fā)技術(shù)的相關(guān)資訊>>


標(biāo)簽: Android開發(fā)從入門到精通 Android學(xué)習(xí)路線圖 Android培訓(xùn) Android開發(fā)工程師 Android視頻教程

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部