Does an asynchronous call always create/call a new thread?

后端 未结 6 1772
迷失自我
迷失自我 2020-12-04 17:45

Does asynchronous call always create a new thread?

Example:

If JavaScript is single threaded then how can it do an async postback? Is it actually blocking u

6条回答
  •  庸人自扰
    2020-12-04 18:27

    The Javascript model is single-threaded. An asynchronous call is not a new thread, but rather interrupts an existing thread. It's analogous to interrupts in a kernel.

    Yes it makes sense to have asynchronous calls with a single thread. Here's how to think about it: When you call a function within a single thread, the state for the current method is pushed onto a stack (i.e. local variables). The subroutine is invoked and eventually returns, at which time the original state is popped off the stack.

    With an asynchronous callback, the same thing happens! The difference is that the subroutine is invoked by the system, not by the current code invoking a subroutine.

提交回复
热议问题