async – stay on the current thread?

If I execute an asynchronous method and it runs, it surely runs on another thread.

No, it typically runs on another thread. It does not surely run on another thread.

Stop thinking about threads for a moment and think about the nature of asynchrony. The nature of asynchrony is:

  • I’ve got some workflow that I am currently executing.
  • I can’t proceed in this workflow until I get information X.
  • I’m going to do something else until I get information X.
  • At some point in the future, once I have X, I’m going to come back to where I left off in my workflow and continue.

Suppose you’re doing your taxes and in the middle of this complicated workflow you have a large addition to perform. You can perform a few operations then remember where you are, and go have lunch. Then come back and perform a few more operations, then remember where you are, and feed the cat. Then come back and perform a few more operations, then remember where you are, and wash the dishes. Then finish off the calculations, and resume where you left off in your workflow.

That’s an asynchronous calculation but it only needed a single worker to do it. Having multiple workers is just a particularly convenient way to do asynchrony, it is not a requirement.

Leave a Comment