3

I just gave a talk about Kotlin's Coroutines and the question arised if Coroutines can always replace Threads or if there also might be disadvantages.

Or the other way round: Is there any area where Coroutines should not be used for?

4

1 回答 1

7

Coroutines are useful for asynchronous programming. When you are writing code that has to wait most of the time for some external events, like it often happens in modern connected user-interfaces and micro-service-oriented backend applications, then coroutines and the concept of Kotlin suspending functions let you write naturally looking and easy-to-understand code that is more scalable than the code with explicit threads.

If you are writing some kind of computation, CPU-intensive code, then you'd find that classical patterns of multithread-programming and parallelism work better.

It does not mean that you cannot use coroutines to parallelize some piece of CPU-intensive application, but you will not get any benefits in either code readability or its performance from doing so.

于 2018-03-01T07:11:29.450 回答