As much as we all love computers they can be very difficult to deal with. Especially as Developers. One of the most crucial aspects of building an application is making sure that it runs in a timely manner. Even the most complex algorithm is no good if it takes 5 hours to run. This problem typically arises when the code is written in a way where it executes block-by-block. This is where Asynchronous Programming can save the day.
Asynchronous Programming is basically a form of parallel programming that allows a block of code to execute on its own separate from the main application thread. Once the block of asynchronous code is complete it will notify the main thread. Now let’s put this logic into an example all of us can understand.
The next time you cook breakfast try to make everything individually. For example if you wanted to make scrambled eggs, sausage, toast and a cup of juice you would have to make the eggs first but you can’t work on other components of your breakfast. So now that the eggs are complete you can finally begin frying the sausages. Let’s say that sausages take 10 minutes. That means that within those 10 minutes you can’t simply put the bread in the toaster to get your toast. As you can see this can end up being very time consuming.
With Asynchronous Programming the workflow of executing code or making breakfast saves a lot of time and headache for the user. Now that we are operating asynchronously let’s take a look at the process.
- Warm the pan for eggs.
- Warm another pan for sausages.
- Depending on which pan warms up first, put the respected item in the pan.
- Cook eggs.
- Flip sausage.
- Put bread in a toaster.
- Flip sausage.
- Put jelly on toast.
- Put eggs, sausage, and toast on a plate.
- Pour juice.
Making breakfast asynchronously will save you about half the time and trouble compared to performing in a synchronous manner. In my next article I will explain how to implement this concept into lines of code while using the Await and Async keywords so keep on the lookout for part 2!