Forget rxjava. You should try kotlin's collaboration. How can programmers solve the midlife crisis

mb61c155e054afb 2022-02-13 09:05:06 阅读数:735

forget rxjava. rxjava try kotlin

(5) Coprocessor fit io Intensive program , Multithreading is suitable for computing intensive programs ( For multi-core cpu The situation of ). Most of the time when you request to read or write files on the network , In this case, you should prefer coprocessing to multithreading , First, most of these operations do not take advantage of cpu Calculate, but wait for the data to read and write , Secondly, because the execution efficiency of collaborative process is high , Subroutine switching is not thread switching , Is controlled by the program itself , therefore , There is no overhead for thread switching , Ratio to multithreading , The more threads there are , The greater the performance advantage of the coroutine . (6) Using a coroutine, asynchronous code can be called sequentially , Avoid going back to hell .

2. It is easy to use

Here I'm going to imitate a network request , Click on button Send network request , Display a progressbar Spin , Return the next result textview Show results and hide progressbar First look at the layout file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android=" http://schemas.android.com/apk/res/android"
xmlns:tools=" http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView br/>android:id="@+id/timeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

 

 <Button br/>android:id="@+id/sendBT"
android:layout_width="wrap_content"
 android:text="SEND"
android:layout_gravity="center"/>
<ProgressBar
android:layout_gravity="center"
android:visibility="gone"
android:id="@+id/loadingPB"
br/>android:layout_height="wrap_content"
android:text="SEND"
android:layout_gravity="center"/>
<ProgressBar
android:layout_gravity="center"
android:visibility="gone"
android:id="@+id/loadingPB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

 

</FrameLayout>

One Button, One TextView, One ProgressBar

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sendBT.setOnClickListener {

coroutineSend()

}
}

private fun coroutineSend() {
val uiScope = CoroutineScope(Dispatchers.Main)
uiScope.launch {
loadingPB.visibility = View.VISIBLE
val deffer = async(Dispatchers.Default) {
getCoroutineResult()
}
val coroutineResult = deffer.await()
timeTV.text = "get $coroutineResult"
loadingPB.visibility = View.GONE
}

}

private suspend fun getCoroutineResult(): String {

delay(9000L)
return "coroutine result"
}
}

First, I created a CoroutineScope, All collaborations run on CoroutineScope in , establish CoroutineScop Parameter passed in Dispatchers.Main, This is a scheduler , It determines that the corresponding coroutine uses one or more threads when executing . The collaboration scheduler can limit the execution of a collaboration to a specified thread , Schedule it to run in the process pool or let it run unrestricted . call launch, Started a collaborative process ,launch Method returns a job, call cancel Method can cancel the progress of this collaboration . As you can see, in the cooperation process, we first show loadingPB, And then call async Start another collaborative process , Use at the same time Dispatchers.Default This co process scheduler , It will enable the coroutine to use a DefaultDispatcher-worker-1 Threads , Why do I use async Instead of using launch, Because async Will return a Deferred object , Call its await Method can block the execution flow and return the result after the execution of the coroutine , In this way, you can get a return value , In this async Used in the created collaboration suspend Method

private suspend fun getCoroutineResult(): String {

summary

It's hard to find a job , And the general cycle is relatively long , Sometimes it depends on both personal skills , It depends on luck . Looking for a job for the first time , Although the final result is not satisfactory , But the harvest is far better than offer Big . The next step is to focus on your own shortcomings , Work hard .

  Finally, in order to save everyone's time , I put all the materials I used in my study and the questions and answers I met in the interview into PDF file , Can be shared with friends in need , If you need to send me a private letter 【 Information 】 perhaps  【 Click here 】 Free collection

 《Android Summary of interview review materials 》

If you like the article, please pay attention to 、 give the thumbs-up 、 forward thank you !

 forget Rxjava Well , You should try Kotlin Cooperation of , How do programmers solve the midlife crisis

copyright:author[mb61c155e054afb],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130905042059.html