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í)吧。dependencies{ implementation'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5' implementation"org.jetbrains.kotlinx:kotlinx-coroutines-android:0.19.3" }
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!" } } }
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() } }
launch(UI)
launch(){...}
launch(CommonPool){...}
privatefuncreatePlainPool():ExecutorService{ valthreadId=AtomicInteger() returnExecutors.newFixedThreadPool(defaultParallelism()){ Thread(it,"CommonPool-worker-${threadId.incrementAndGet()}").apply{isDaemon=true} } } privatefundefaultParallelism()=(Runtime.getRuntime().availableProcessors()-1).coerceAtLeast(1)
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Android開發(fā)技術(shù)”的相關(guān)資訊>>