zoukankan      html  css  js  c++  java
  • 异步编程- async和await

    使用目的

    避免阻塞主线程
    提高程序响应能力

    C#中使用

    C# 中的 Async 和 Await 关键字是异步编程的核心。

    运行机制

    疑惑

    The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active.

    答疑

    I explain it in full in my blog post There Is No Thread.
    In summary, modern I/O systems make heavy use of DMA (Direct Memory Access). There are special, dedicated processors on network cards, video cards, HDD controllers, serial/parallel ports, etc. These processors have direct access to the memory bus, and handle reading/writing completely independently of the CPU. The CPU just needs to notify the device of the location in memory containing the data, and then can do its own thing until the device raises an interrupt notifying the CPU that the read/write is complete.
    Once the operation is in flight, there is no work for the CPU to do, and thus no thread.

    使用场景

    程序中耗时I/O操作时适合使用。
    I/O操作不仅包括了直接的文件、网络的读写,还包括数据库操作、Web Service、HttpRequest的调用。  

    参考

    https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/async/index

    https://stackoverflow.com/questions/37419572/if-async-await-doesnt-create-any-additional-threads-then-how-does-it-make-appl

  • 相关阅读:
    图的存储结构(邻接表) 数据结构和算法57
    邻接多重表
    十字链表
    Java类型转换
    okHttp超时报错解决方案
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.paipaixiu/com.example.paipaixiu.MASetSomeThing}: android.view.InflateException: Binary XML file line #19: Attempt to invo
    Oracle nal() 和count(*)的注意点
    RecyclerView实现一个页面有多种item,每个item有多个view,并且可以让任意item的任意view自定义监听,通过接口方法进行触发操作
    session的一些笔记
    Android项目创建.prorperties配置文件和调用方法
  • 原文地址:https://www.cnblogs.com/talentzemin/p/7514443.html
Copyright © 2011-2022 走看看