What does it mean for a method to be asynchronous?

前端 未结 4 1220
一个人的身影
一个人的身影 2021-01-31 21:06

What is an asynchronous method. I think I know, but I keep confusing it with parallelism. I\'m not sure what the difference between an asynchronous method is and what parallel

4条回答
  •  悲&欢浪女
    2021-01-31 21:35

    First off, what is a method and what is a thread? A method is a unit of work that either (1) performs a useful side effect, like writing to a file, or (2) computes a result, like making a bitmap of a fractal. A thread is a worker that performs that work.

    A method is synchronous if in order to use the method -- to get the side effect or the result -- your thread must do nothing else from the point where you request the work to be done until the point where it is finished.

    A method is asynchronous if your thread tells the method that it needs the work to be done, and the method says "OK, I'll do that and I'll call you when it is finished".

    Usually the way an asynchronous method does that is it makes another worker -- it grabs a thread from the pool. This is particularly true if the method needs to make heavy use of a CPU. But not always; there is no requirement that an asynchronous method spins up another thread.

    Does that make sense?

提交回复
热议问题