<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Deeptanshu Malik</title>
    <description>Welcome to my blog.</description>
    <link>https://deeptanshumalik.com/</link>
    <atom:link href="https://deeptanshumalik.com/feed.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>Asynchronous Programming 101</title>
        <description>&lt;p&gt;In this blog post I’ll give a top-down introduction to asynchronous programming in C#. This post may be useful for anyone new to C# and for students who have learnt about threads and concurrency. I believe that a top-down approach will be ideal as it’ll first show &lt;em&gt;what&lt;/em&gt; async programming is so that you can be interested in the &lt;em&gt;how&lt;/em&gt; and &lt;em&gt;why&lt;/em&gt; of it.&lt;/p&gt;

&lt;p&gt;The code used in this blog is available on my GitHub &lt;a href=&quot;https://github.com/DeeptanshuM/AsyncProgramming101/blob/master/AsyncProgrammingDemo/Program.cs&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/sfFerry2.jpg#banner&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;San Francisco, on some lazy Saturday afternoon in July, 2018.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;fundamentals&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;a-quick-review-of-the-fundamentals&quot;&gt;A quick review of the fundamentals&lt;/h2&gt;

&lt;p&gt;&lt;a name=&quot;fundamentals_latency&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;latency--throughput&quot;&gt;Latency &amp;amp; Throughput&lt;/h4&gt;
&lt;p&gt;Latency is a measure of units of time taken to do work. Throughput is measure of the units of work done per unit of time.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;fundamentals_concurrency&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;concurrency&quot;&gt;Concurrency&lt;/h4&gt;
&lt;p&gt;Concurrency means multiple computations are happening at the same time&lt;sup&gt;[1]&lt;/sup&gt;. It is different than parallelism as explained in &lt;a href=&quot;https://stackoverflow.com/questions/1050222/what-is-the-difference-between-concurrency-and-parallelism&quot;&gt;this&lt;/a&gt; stackoverflow answer:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Concurrency: A condition that exists when at least two threads are making progress. A more generalized form of parallelism that can include time-slicing as a form of virtual parallelism&lt;sup&gt;[2]&lt;/sup&gt;.&lt;/li&gt;
  &lt;li&gt;Parallelism: A condition that arises when at least two threads are executing simultaneously&lt;sup&gt;[2]&lt;/sup&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a name=&quot;fundamentals_tasks&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;tasks&quot;&gt;Tasks&lt;/h4&gt;
&lt;p&gt;A Task is an abstraction that represents an operation&lt;sup&gt;[3]&lt;/sup&gt;. This operation may or may not be backed by more than one thread and it may or may not be executed concurrently. We can use Tasks instead of working with threads directly so that we don’t need to manually, explicitly implement locking, joining and synchronizing operations for threads and we don’t need to manually create or destroy threads directly either. By default in C# Tasks use threads from a Thread Pool. A Thread Pool is a design pattern that maintains multiple threads such that they can be reused/recycled. We can use this design pattern is to reduce the latency of creating, starting and deleting threads repeatedly.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;fundamentals_async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;synchronous-versus-asynchronous-operations&quot;&gt;Synchronous versus Asynchronous Operations&lt;/h4&gt;
&lt;p&gt;A synchronous operation will do its work before returning to the caller whereas an asynchronous operation may do some (including all) part of its work after returning to the caller&lt;sup&gt;[3]&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;fundamentals_examples&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;an-analogy-to-wrap-up-these-fundamental-concepts&quot;&gt;An analogy to wrap up these fundamental concepts&lt;/h4&gt;
&lt;p&gt;Let’s consider a laundromat. If you put clothes in two machines at literally the same time, for example one washing machine with one hand and another washing machine with the other hand &lt;em&gt;at the same time&lt;/em&gt;, then you’re loading the clothes in &lt;em&gt;parallel&lt;/em&gt;. But if you load clothes simultaneously in two washing machine such that at a given instance in time you are only loading clothes in one washing machine at a time, then you are loading the clothes &lt;em&gt;concurrently&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The time taken by the washing machine to wash one load of clothes will be the &lt;em&gt;latency&lt;/em&gt; of a washing machine. To reduce time spent waiting for n loads of clothes to be washed, instead of running one machine n times, n machines can be run at the same time: the work done by a machine takes the same amount of time in both the cases but the difference is that the in the latter case amount of work done at in the same amount of time (n loads washed at the same time) is more, i.e., the &lt;em&gt;throughput&lt;/em&gt; is more.&lt;/p&gt;

&lt;p&gt;To do all these things maybe you asked the person at the front desk for quarters: you couldn’t do anything at the time, you were at the front of a queue and you gave the person two dollar bills and waited for them to return quarters. This was a &lt;em&gt;synchronous&lt;/em&gt; operation. When a washing machine is running you still retain control- you can go ahead and load and start running another washing machine while the first one is still running - this makes the running of a washing machine an &lt;em&gt;asynchronous&lt;/em&gt; operation.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;demo&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;async-programming-demo&quot;&gt;Async Programming Demo&lt;/h2&gt;

&lt;p&gt;Let’s look at a demo to understand how async functions are defined and called and how async programming impacts program latency by improving throughput.&lt;/p&gt;

&lt;p&gt;Asynchronous functions in the C# are defined by adding the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; keyword to the function definition. They can be called like any other functions. Async Functions in C# must return a Task or some type of Task. In the demo program below we will see how these tasks can be initiated, how they can be run synchronously and asynchronously and how the code flow can be stopped to wait for the result of a task by using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;demo_funcs&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;dummy-synchronous-and-asynchronous-functions&quot;&gt;Dummy Synchronous and Asynchronous Functions&lt;/h4&gt;

&lt;p&gt;Let’s implement a dummy async function:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;private async Task&amp;lt;int&amp;gt; longRunningMultiplyBy10(int number)
{
    Console.WriteLine(&quot;# Initiated long running op on for the input number=&quot; + number + &quot; from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId + &quot; #&quot;);

    //100 delays of 100 ms = 10 seconds
    for (int i = 0; i &amp;lt; 100 ; i++)
    {
        await Task.Delay(100);
    }

    Console.WriteLine(&quot;# Completed long running op on for the input number=&quot; + number + &quot; from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId + &quot; #&quot;);
    return number * 10;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this function we use the native asynchronous &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Delay&lt;/code&gt; function by calling it asynchronously using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt; keyword to wait for a total of 10 seconds and then multiply the input number by 10 and return it. We will now see the difference in performance when it is called synchronously and asynchronously. To do so we will implement functions to take to input numbers, perform the dummy long running operation on each of them and return their sum.&lt;/p&gt;

&lt;p&gt;The function to call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;longRunningMultiplyBy10&lt;/code&gt; synchronously is:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;private int syncMultiplyBy10AndAdd(int num1, int num2)
{
    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    int a = longRunningMultiplyBy10(num1).Result;

    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    int b = longRunningMultiplyBy10(num2).Result;

    Console.WriteLine(&quot;Executing return from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    return a + b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s write a function that uses asynchronous programming:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;private async Task&amp;lt;int&amp;gt; asyncMultiplyBy10AndAddAwaitImmediately(int num1, int num2)
{
    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    int a = await longRunningMultiplyBy10(num1);

    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    int b = await longRunningMultiplyBy10(num2);

    Console.WriteLine(&quot;Executing return from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    return a + b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can observe here that we don’t need to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt; on the long running operation until we need to access the Task’s result— let’s write a function to reflect this and observe its performance as well.&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;private async Task&amp;lt;int&amp;gt; asyncMultiplyBy10AndAddAwaitAtEnd(int num1, int num2)
{
    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    Task&amp;lt;int&amp;gt; a = longRunningMultiplyBy10(num1);

    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    Task&amp;lt;int&amp;gt; b = longRunningMultiplyBy10(num2);

    Console.WriteLine(&quot;Executing return from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    return await a + await b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a name=&quot;demo_helpers&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;helper-functions-to-call-dummy-functions-and-record-their-performance&quot;&gt;Helper functions to call dummy functions and record their performance&lt;/h4&gt;
&lt;h5 id=&quot;this-section-is-unneccessary-and-possibly-a-distraction-skip-to-next-section&quot;&gt;This section is unneccessary and possibly a distraction, skip to &lt;a href=&quot;#demo_results&quot;&gt;next&lt;/a&gt; section&lt;/h5&gt;

&lt;p&gt;We will make a call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;demo&lt;/code&gt; to call our demo functions and measure their performance as follows:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;private void demo()
{
    Console.WriteLine(&quot;##### Testing synchronous code #####&quot;);
    demoSyncFunction(syncMultiplyBy10AndAdd);
    Console.WriteLine(&quot;####################################&quot; + Environment.NewLine);

    Console.WriteLine(&quot;##### Testing await immediately #####&quot;);
    demoAsyncFunction(asyncMultiplyBy10AndAddAwaitImmediately);
    Console.WriteLine(&quot;####################################&quot; + Environment.NewLine);
    Console.WriteLine();

    Console.WriteLine(&quot;##### Testing await at end #####&quot;);
    demoAsyncFunction(asyncMultiplyBy10AndAddAwaitAtEnd);
    Console.WriteLine(&quot;####################################&quot; + Environment.NewLine);
    Console.WriteLine();
}

private void demoSyncFunction(Func&amp;lt;int, int, int&amp;gt; asyncDemoFunction)
{
    int num1 = 15;
    int num2 = 33;
    int result;
    Stopwatch stopWatch = new Stopwatch();

    stopWatch.Start();
    result = asyncDemoFunction(num1, num2);
    stopWatch.Stop();
    displayRuntime(stopWatch.Elapsed);

}

private void demoAsyncFunction(Func&amp;lt;int, int, Task&amp;lt;int&amp;gt;&amp;gt; asyncDemoFunction)
{
    int num1 = 15;
    int num2 = 33;
    Stopwatch stopWatch = new Stopwatch();

    stopWatch.Start();

    int result = asyncDemoFunction(num1, num2).Result;

    stopWatch.Stop();

    displayRuntime(stopWatch.Elapsed);
}

private void displayRuntime(TimeSpan ts)
{
    string elapsedTime = String.Format(&quot;{0:00}min:{1:00}s.{2:00}ms&quot;,
        ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);

    Console.WriteLine(&quot;Runtime: &quot; + elapsedTime);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a name=&quot;demo_results&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;the-results&quot;&gt;The Results&lt;/h4&gt;

&lt;p&gt;The output is as follows:&lt;/p&gt;

&lt;p&gt;For the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syncMultiplyBy10AndAdd&lt;/code&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;private int syncMultiplyBy10AndAdd(int num1, int num2)
{
    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    int a = longRunningMultiplyBy10(num1).Result;

    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    int b = longRunningMultiplyBy10(num2).Result;

    Console.WriteLine(&quot;Executing return from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    return a + b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The output is:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;##### Testing synchronous code #####
Calling long running Op from Thread 1
# Initiated long running op on for the input number=15 from Thread 1 #
# Completed long running op on for the input number=15 from Thread 8 #
Calling long running Op from Thread 1
# Initiated long running op on for the input number=33 from Thread 1 #
# Completed long running op on for the input number=33 from Thread 8 #
Executing return from Thread 1
Runtime: 00min:20s.08ms
####################################
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We observe that Thread 1 began executing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syncMultiplyBy10AndAdd&lt;/code&gt;. When it called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;longRunningMultiplyBy10&lt;/code&gt; the execution of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syncMultiplyBy10AndAdd&lt;/code&gt; was blocked. Thread 1 then began the execution of the long running operation which ran asynchronously. As the function ran asynchronously Thread 1 was released from the task of executing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;longRunningMultiplyBy10 &lt;/code&gt;. However, because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syncMultiplyBy10AndAdd&lt;/code&gt; is synchronous, when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;longRunningMultiplyBy10&lt;/code&gt; completed Thread 1 remained responsible for executing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syncMultiplyBy10AndAdd&lt;/code&gt; throughout. We note that the function took &lt;strong&gt;20s.08ms&lt;/strong&gt; to execute.&lt;/p&gt;

&lt;p&gt;Now when the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;asyncMultiplyBy10AndAddAwaitImmediately&lt;/code&gt; was executed:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;private async Task&amp;lt;int&amp;gt; asyncMultiplyBy10AndAddAwaitImmediately(int num1, int num2)
{
    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    int a = await longRunningMultiplyBy10(num1);

    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    int b = await longRunningMultiplyBy10(num2);

    Console.WriteLine(&quot;Executing return from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    return a + b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The output was&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;##### Testing await immediately #####
Calling long running Op from Thread 1
# Initiated long running op on for the input number=15 from Thread 1 #
# Completed long running op on for the input number=15 from Thread 7 #
Calling long running Op from Thread 7
# Initiated long running op on for the input number=33 from Thread 7 #
# Completed long running op on for the input number=33 from Thread 8 #
Executing return from Thread 8
Runtime: 00min:20s.04ms
####################################
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We observe that Thread 1 began executing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;asyncMultiplyBy10AndAddAwaitImmediately&lt;/code&gt;. The difference this time is that because the call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;longRunningMultiplyBy10&lt;/code&gt; was asynchronous Thread 1 was released from the task of executing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;asyncMultiplyBy10AndAddAwaitImmediately&lt;/code&gt; and another thread from the thread pool, Thread 7, resumed the execution. We note that the function took &lt;strong&gt;20s.04ms&lt;/strong&gt;. This is definitely faster than the synchronous-calls implementation and we can intuitively see how at scale in the real world the improvement in latency will be significant.&lt;/p&gt;

&lt;p&gt;We can also observe an inefficiency: we &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt;ed the completion of the long running operation earlier than when we needed to use its result. So although we used the thread pool to assign the task of executing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;asyncMultiplyBy10AndAddAwaitImmediately&lt;/code&gt; more efficiently to a thread by using async programming, we didn’t leverage the full power of async programming to allow long running operations (in this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;longRunningMultiplyBy10&lt;/code&gt;) to run &lt;strong&gt;concurrently&lt;/strong&gt; with the execution of the caller function (in this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;asyncMultiplyBy10AndAddAwaitImmediately&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;When we address this inefficiency and don’t &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt; the results of the tasks until we need to use them and then test our function&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;private async Task&amp;lt;int&amp;gt; asyncMultiplyBy10AndAddAwaitAtEnd(int num1, int num2)
{
    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    Task&amp;lt;int&amp;gt; a = longRunningMultiplyBy10(num1);

    Console.WriteLine(&quot;Calling long running Op from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    Task&amp;lt;int&amp;gt; b = longRunningMultiplyBy10(num2);

    Console.WriteLine(&quot;Executing return from Thread &quot; + System.Threading.Thread.CurrentThread.ManagedThreadId);
    return await a + await b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;we get the following result:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;##### Testing await at end #####
Calling long running Op from Thread 1
# Initiated long running op on for the input number=15 from Thread 1 #
Calling long running Op from Thread 1
# Initiated long running op on for the input number=33 from Thread 1 #
Executing return from Thread 1
# Completed long running op on for the input number=33 from Thread 8 #
# Completed long running op on for the input number=15 from Thread 6 #
Runtime: 00min:10s.04ms
####################################
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The execution of the long running operation started as soon as it was called. The executions of the caller function and the long running operations continued concurrently. And the runtime of the effectively asynchronously coded function was only &lt;strong&gt;00min:10s.04ms&lt;/strong&gt;: that’s a solid &lt;strong&gt;50%&lt;/strong&gt; latency improvement over the synchronous implementation that called 2 long running operations. Since the asynchronously coded function operation calls the 2 long running ops in a way that allows them to run concurrently, the 50% improvement is as expected.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;whatandhow&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;what-is-async-programming-and-how-it-works&quot;&gt;What is Async Programming and How it Works&lt;/h2&gt;

&lt;p&gt;As we have now observed, async programming allows us to effectively utilize threads to run tasks concurrently. When an async function is called the control is returned to the caller function immediately (as soon as the synchronous part of the asynchronous function is completed) and the execution of the caller and the called functions continues concurrently on separate threads until the caller function must &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt; the completion of the called function. This helps improve the overall program latency (the total time the program takes to run) by increasing the throughput (the work done per unit of time) by effectively utilizing the thread pool to assign instances of tasks to different threads that run concurrently.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;references&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;references&quot;&gt;References&lt;/h3&gt;
&lt;p&gt;[1] https://web.mit.edu/6.005/www/fa14/classes/17-concurrency/ &lt;br /&gt;
[2] Defining Multithreading Terms &lt;a href=&quot;https://docs.oracle.com/cd/E19455-01/806-5257/6je9h032b/index.html&quot;&gt;Oracle Multithreaded Programming Guide&lt;/a&gt; &lt;br /&gt;
[3] Chapter 14 Concurrency &amp;amp; Asynchrony of &lt;a href=&quot;https://www.amazon.com/C-5-0-Nutshell-Definitive-Reference-dp-1449320104/dp/1449320104/ref=mt_paperback?_encoding=UTF8&amp;amp;me=&amp;amp;qid=&quot;&gt;C# 5.0 in a Nutshell: The Definitive Reference Fifth Edition by Joseph Albahari, Ben Albahari (Author)&lt;/a&gt; &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;A shoutout to &lt;a href=&quot;https://social.technet.microsoft.com/profile/Hilton+Lange&quot;&gt;Hilton Lange&lt;/a&gt;: his explanation of asynchronous programming and demo async program inspired me to write this blog.&lt;/p&gt;
</description>
        <pubDate>Wed, 03 Jul 2019 00:00:00 +0000</pubDate>
        <link>https://deeptanshumalik.com/2019/07/03/async-programming-demo/</link>
        <guid isPermaLink="true">https://deeptanshumalik.com/2019/07/03/async-programming-demo/</guid>
      </item>
    
      <item>
        <title>Pictures I clicked in the past 12 months that I am in awe of today</title>
        <description>&lt;p&gt;I was recently going through pictures that I had clicked in the past 12 months. As I did so I came across pictures I am in awe of today. This post is a collection of some of those pictures.&lt;/p&gt;

&lt;h3 id=&quot;about-these-pictures&quot;&gt;About these pictures&lt;/h3&gt;
&lt;p&gt;(Or click &lt;a href=&quot;#pictures&quot;&gt;here&lt;/a&gt; to jump directly to the pictures)&lt;/p&gt;

&lt;p&gt;I’ve shot them all on my iPhone X. I’ve cropped some and rotated some a bit to make sure that they are not tilted. But I have not edited them beyond this. I have not applied any filters or changed the contrast etc. My goal when I take a picture is to preserve that moment to see the picture again later exactly as I saw it. Of course, I’ve made the implicit assumption here that how my eyes perceive something and my phone camera captures it are adequately similar. Also, I feel that if I make even the slightest edit to my picture like such as changing its contrast or brightening it - that picture is sufficiently so far removed from reality that it might as well be a cartoon sketch.&lt;/p&gt;

&lt;p&gt;There’s some degree of serendipity involved which led to each picture. In almost all cases I’ve taken the picture on a walk that I went on on a whim or when I was going somewhere, whether commuting to work or intentionally to a tourist spot or to do some recreational activity, and I just looked outside outside the cab window or kept my eyes open and remained In the present on my walk and saw a sight I wanted to capture.&lt;/p&gt;

&lt;p&gt;So, in conclusion, this post is a collection of pictures from the past 12 months that looking back I’m proud I clicked. It’s not a collection of pictures of moments of pride or happiness from the past 12 months.  This post is also a collection of pictures that I had taken during my travels in the past 12 months. However I don’t intend for it to be a travel diary by any means. I don’t want to write a travel blog. At most, perhaps someday in the distant future, I will write a series of city-by-city reviews of the American cities that I’ve visited.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;pictures&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-pictures&quot;&gt;The Pictures&lt;/h2&gt;

&lt;h3 id=&quot;skylines-of-great-american-cites--pictures-of-american-prosperity--capitalism-creates-a-beautiful-world--i-couldnt-choose-a-title&quot;&gt;Skylines of great American cites / Pictures of American prosperity / Capitalism creates a beautiful world / (I couldn’t choose a title)&lt;/h3&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/sfFerry1.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;San Francisco.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/sfBuilding.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;I was walking in Sf downtown my first weekend there after I had arrived in the city. After having spent a semester in West Lafayette, IN, it was therapeutic to be in a densely packed urban environment.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/sfFerry2.jpg#banner&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;I took this picture from another ferry. There was no plan to go on a ferry. We were at Pier 39, saw a tourist ferry ride being advertised, decided to go on it. The color of the water, the sky, the city skyline and the bridge all make this picture.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/LA.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;LA skyline.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/nyc.jpg#banner&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;The greatest view in the world. Manhattan skyline.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/wtc.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;WTC.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/seattlePano.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Seattle panorama.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/seattle.jpg#banner&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Seattle on a rare not-gray-and-damp day.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/SfSunset.jpg#banner&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Evening sunlight and SF skyline. And one prominent red lamppost but what the hell.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h4 id=&quot;sky-pictures&quot;&gt;Sky Pictures&lt;/h4&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/dawn.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;5:45 am sky in Seattle.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/MidwestSunset.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Evening Midwestern sky. The last time I played tennis at Purdue outdoors was under this sky.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/CaliSky.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;California blue.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/MenloPark.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Menlo Park, CA, Caltrain station in the evening.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/midwestWinterSky.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Cold January day in Indiana.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h4 id=&quot;miscellaneous&quot;&gt;Miscellaneous&lt;/h4&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/santamonica.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Santa Monica Beach. The pictured turned out to be this good purely by accident.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/snow.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Snow @ Purdue.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/tennis.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Schwartz Tennis Center at Purdue University. Walk down this corridor to a tennis court.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/bridge.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;I was in a train, saw this bridge approaching, took a picture of it.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/tree.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Fall color at Purdue.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/commute.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Sunnyvale in the evening.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/ships.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Blue California sky. Blue California water. 4 identical ships. Taken on a ferry ride from Redwood City to SF.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/mountain.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Mount Rainier as seen from the floating bridge that connects Seattle and Bellevue.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/port.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Worst snow storm in 20 years in Seattle but the lights at this port are on and work continues. Amazing.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/seattleBuilding.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;A random building in downtown Seattle.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;em&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/chicago.jpg&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Chicago financial district the day before Christmas.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h4 id=&quot;nothing-special-about-this-picture-itself-but-i-felt-compelled-to-include-it&quot;&gt;Nothing special about this picture itself but I felt compelled to include it&lt;/h4&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;img src=&quot;https://deeptanshumalik.com/images/May_11_19/boston.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;Warren Tavern in Boston. George Washington drank here.&lt;/p&gt;
</description>
        <pubDate>Sat, 11 May 2019 00:00:00 +0000</pubDate>
        <link>https://deeptanshumalik.com/2019/05/11/Pictures-I-clicked-in-the-past-12-months-that-I-am-in-awe-of-today/</link>
        <guid isPermaLink="true">https://deeptanshumalik.com/2019/05/11/Pictures-I-clicked-in-the-past-12-months-that-I-am-in-awe-of-today/</guid>
      </item>
    
      <item>
        <title>A Most Life Affirming Article by Riva Tez</title>
        <description>&lt;center&gt;&lt;blockquote class=&quot;twitter-tweet&quot; data-lang=&quot;en&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;I read this 3 days ago and it has made a not-insignificant improvement to my quality of life. Excellent article by &lt;a href=&quot;https://twitter.com/rivatez?ref_src=twsrc%5Etfw&quot;&gt;@rivatez&lt;/a&gt; &lt;a href=&quot;https://t.co/XmQ3bQWsEM&quot;&gt;https://t.co/XmQ3bQWsEM&lt;/a&gt;&lt;/p&gt;&amp;mdash; Deeptanshu Malik (@deeptanshuMalik) &lt;a href=&quot;https://twitter.com/deeptanshuMalik/status/1118393245443215360?ref_src=twsrc%5Etfw&quot;&gt;April 17, 2019&lt;/a&gt;&lt;/blockquote&gt; &lt;script async=&quot;&quot; src=&quot;https://platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;&lt;/center&gt;

&lt;p&gt;From the article:&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;“&lt;i&gt;To be in awe&lt;/i&gt;. That’s what the days are for.”&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;“To think that some of the best days of our lives haven’t happened yet, no matter how bad the current day has been, how bad the situation, how poor you currently are or how anguished.”&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;“Another day is another opportunity for kindness. It’s another day for passion. It’s another day to carve out your reality.&quot;&lt;/p&gt;

&lt;h3 id=&quot;a-new-day-is-a-new-opportunity-to-carve-out-your-reality&quot;&gt;A new day is a new opportunity to carve out your reality&lt;/h3&gt;

&lt;p&gt;Read the whole article here: &lt;a href=&quot;http://idealistmag.com/sleep/6am/&quot;&gt;http://idealistmag.com/sleep/6am/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And you can find the author of this article, Riva-Melissa Tez,  on Twitter here: &lt;a href=&quot;https://twitter.com/rivatez&quot;&gt;@rivatez&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Wed, 17 Apr 2019 00:00:00 +0000</pubDate>
        <link>https://deeptanshumalik.com/2019/04/17/a-most-life-affirming-article-by-riva-tez/</link>
        <guid isPermaLink="true">https://deeptanshumalik.com/2019/04/17/a-most-life-affirming-article-by-riva-tez/</guid>
      </item>
    
      <item>
        <title>Sky Colors in US Tech Hubs</title>
        <description>&lt;p&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/March_3_19/dataVizArticleBannerImage.jpg&quot; alt=&quot;banner image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article is also available as a step-by-step tutorial on my Github &lt;a href=&quot;https://github.com/DeeptanshuM/dataVisualization_WeatherInUSATechHubs/blob/master/DailySkyColorCityVisualization.ipynb&quot;&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;motivation-for-thisarticle&quot;&gt;Motivation for this article&lt;/h3&gt;
&lt;p&gt;I’ve recently moved to Seattle, WA. Before moving here I had read that the city is known for its gray skies. It’s also famous for having overcast clouds more than 200 days per year. In the few weeks that I’ve been here I’m reminded of this fact almost daily. I became curious: how often does Seattle have gray skies compared to other cities? Because I’m in the tech industry, I was curious about those other cities in the country that are also tech-hubs. I had a nagging suspicion that that the difference in the daily sky colors of Seattle and other tech hubs won’t be too different as the other tech-hubs are also coastal cities. &lt;/p&gt;

&lt;p&gt;I had spent the last summer in the Bay Area for an internship and I had noticed that the skies in San Francisco were gray more often than they were blue. I had not expected that at all: I had imagined that San Francisco has sunny days all year round. The reality was that mist set in most days and as a consequence the sky was usually a light shade of gray. This recollection led me to think that perhaps the gray-sky weather in Seattle is not that different from other cities where I could have possibly been if I were not in Seattle, i.e., other tech hubs.&lt;/p&gt;

&lt;p&gt;So I decided to make a simple data visualization: I made vertical bar charts where from left to right the most frequently occurring color of the sky everyday of the year is shown. I made these bar charts for the major tech-hubs I identified in the weather database I found to visually see just how different the color of the sky is in these cities throughout the year.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/March_3_19/SkyColor_SF.jpeg&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://deeptanshumalik.com/images/March_3_19/SkyColor_Seattle.jpeg&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://deeptanshumalik.com/images/March_3_19/SkyColor_NYC.jpeg&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://deeptanshumalik.com/images/March_3_19/SkyColor_Boston.jpeg&quot; alt=&quot;&quot; /&gt;
Because this project is based on open data provided by an Israeli (data from an &lt;a href=&quot;https://www.kaggle.com/selfishgene/historical-hourly-weather-data#weather_description.csv&quot;&gt;open database on Kaggle&lt;/a&gt; provided by &lt;a href=&quot;https://davidbeniaguev.com/&quot;&gt;David Beniaguev&lt;/a&gt;. ) and Tel-Aviv is definitely a major tech-hub also, a bonus plot:
&lt;img src=&quot;https://deeptanshumalik.com/images/March_3_19/SkyColor_TelAviv.jpeg&quot; alt=&quot;&quot; /&gt;
In the data visualizations above you’ll see the color of the sky in the tech hubs of western hemisphere everyday of the year throughout the year. The plots are bar plots. Each bar in the bar chart represents one day, there are 365 bars corresponding to the days of the year, and each bar is a color that is the most frequently occurring color of the sky on that day in that city over the past few years.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;methodology&quot;&gt;Methodology&lt;/h3&gt;
&lt;p&gt;I found a database of hourly weather data for cities on Kaggle.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://deeptanshumalik.com/images/March_3_19/rawdata.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;
&lt;i&gt;Raw data file from &lt;a href=&quot;https://www.kaggle.com/selfishgene/historical-hourly-weather-data#weather_description.csv&quot;&gt;Kaggle&lt;/a&gt;&lt;/i&gt;
&lt;/p&gt;

&lt;p&gt;I extracted weather data for one city at a time from the raw data file. I mapped each date to day of the year. I calculated day-of-year from the year-month-day value. For example, 2015-01–01 is mapped to 1 as 1st Jan is the second day of the year and 2016-01–02 is mapped to 2 as 2nd Jan is the 2nd day of the year. I discarded data for what qualifies, roughly, as night time. As we are concerned with the differences in the colors of the sky and the color of the sky is always same at night we can disregard weather data for night time.&lt;/p&gt;

&lt;p&gt;For each day-of-year for most hours I had multiple weather descriptions. Because I wanted to see what the sky looks like most often I chose the mode of the weather description value for each hour, i.e., I chose the most frequently reoccurring or most common weather description for each hour of each day. Similarly, because I wanted to visualize one color for one day I filtered the data further and discarded the hour values to keep only the day-of-year values and the most frequently reoccurring or most common weather description for each day of the year.&lt;/p&gt;

&lt;p&gt;I now had a column of day-of-year and a corresponding column of weather descriptions.&lt;/p&gt;

&lt;p&gt;At the very beginning of my data processing I had listed all the distinct weather descriptions that were there in the raw data for all cities. I then stored key-value pairs of the weather descriptions and their rgb color values. I calculated the rgb values using my Mac’s Digital Color Meter. I searched on Google Images for each distinct weather description (except for the different types of thunderstorms, I repeated the rgb values for those) and then used the Digital Color Meter to record the rgb value of the color of the sky from a relevant image search result. The process was extremely tedious, inefficient and subjective but perfectly adequate. I merged this information with the column of day-of-year and a corresponding column of weather descriptions such that the weather descriptions now had their color values associated with them and therefore, each day-of-year had a color value associated with it also.&lt;/p&gt;

&lt;p&gt;Finally, I made a vertical bar chart such that each bar represents one day, the bars in the chart represent day 1 through day 366 of the year and the color of each bar corresponds to, what is according to my subjective opinion, the color of the sky on that day as per the weather description of that corresponding day of the year.&lt;/p&gt;

&lt;p&gt;I used Pandas for my data analysis and Plot.ly to make the bar charts. You can see all the code, formatted as a step-by-step tutorial, for this project in this &lt;a href=&quot;https://github.com/DeeptanshuM/dataVisualization_WeatherInUSATechHubs/blob/master/DailySkyColorCityVisualization.ipynb&quot;&gt;Jupyter NotebooK&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;This project is based on data from an &lt;a href=&quot;https://www.kaggle.com/selfishgene/historical-hourly-weather-data#weather_description.csv&quot;&gt;Open Database on Kaggle&lt;/a&gt; provided by &lt;a href=&quot;https://davidbeniaguev.com/&quot;&gt;David Beniaguev&lt;/a&gt;. Thank you David!&lt;/p&gt;

</description>
        <pubDate>Sun, 03 Mar 2019 00:00:00 +0000</pubDate>
        <link>https://deeptanshumalik.com/2019/03/03/Sky-Colors-in-US-Tech-Hubs/</link>
        <guid isPermaLink="true">https://deeptanshumalik.com/2019/03/03/Sky-Colors-in-US-Tech-Hubs/</guid>
      </item>
    
      <item>
        <title>Concurrent Programming Reading list</title>
        <description>&lt;p&gt;This blog post is a collection of articles about threads, locks and concurrency.&lt;/p&gt;

&lt;h3 id=&quot;some-context&quot;&gt;Some context&lt;/h3&gt;

&lt;p&gt;I’ve had a lot of time to kill in the last few weeks so I recently &lt;a href=&quot;https://www.coursera.org/account/accomplishments/specialization/WKZ58NS8RZB7&quot;&gt;completed&lt;/a&gt; a series of 3 classes, a &lt;a href=&quot;https://www.coursera.org/specializations/pcdp&quot;&gt;specialization&lt;/a&gt;, on Coursera — Parallel, Concurrent, and Distributed Programming in Java Specialization.&lt;/p&gt;

&lt;p&gt;The first two classes, Parallel Programming in Java and Concurrent Programming in Java were good revisions of material I had studied in one of my computer engineering classes at Purdue exactly an year ago. In the Distributed Programming in Java class I used Hadoop and Spark for the first time and revised more material related to networking and about different approaches to combine distribution with multithreading that I had learned in my classes at Purdue. This class was mostly about high-level abstractions and frameworks that are useful in distributed systems.&lt;/p&gt;

&lt;p&gt;In the Concurrent Programming class I found various articles listed under the ‘Optional Reading’ section that were extremely resourceful. In college I had studied concurrency in my computer architecture class. I had referred to the textbook and my professor’s slides. I don’t have that course textbook or my professor’s slides anymore. So I’ve made this list of articles to be my go-to resource for fundamentals of concurrency.&lt;/p&gt;

&lt;h3 id=&quot;the-list&quot;&gt;The list&lt;/h3&gt;

&lt;h4 id=&quot;threads&quot;&gt;Threads&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Thread_(computing)&quot;&gt;Wikipedia article on Threads&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;structured-locks&quot;&gt;Structured Locks&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html&quot;&gt;Tutorial on Intrinsic Locks and Synchronization in Java&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html&quot;&gt;Tutorial on Guarded Blocks in Java&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;unstructured-locks&quot;&gt;Unstructured Locks&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.html&quot;&gt;Tutorial on Lock Objects in Java&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;liveness&quot;&gt;Liveness&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Deadlock&quot;&gt;Wikipedia article on Deadlock and Livelock&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Java examples of &lt;a href=&quot;https://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html&quot;&gt;deadlock due to synchronized methods&lt;/a&gt; and &lt;a href=&quot;https://docs.oracle.com/javase/tutorial/essential/concurrency/starvelive.html&quot;&gt;starvation and livelock&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;critical-sections&quot;&gt;Critical sections&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.coursera.org/learn/concurrent-programming-in-java/supplement/gaQ9x/2-1-lecture-summary&quot;&gt;Wikipedia article on Critical Sections&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Atomicity_(database_systems)&quot;&gt;Wikipedia article on Atomicity&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;object-based-isolation&quot;&gt;Object-Based Isolation&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Monitor_(synchronization)&quot;&gt;Wikipedia article on Monitors&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;atomic-variables-in-java&quot;&gt;Atomic Variables in Java&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html&quot;&gt;Tutorial on Atomic Integers in Java&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ibm.com/developerworks/library/j-jtp11234/&quot;&gt;Article&lt;/a&gt; in Java theory and practice series on Going atomic.
    &lt;ul&gt;
      &lt;li&gt;&lt;i&gt; “By exposing new low-level coordination primitives internally, and providing a set of public atomic variable classes, it now becomes practical, for the first time, to develop wait-free, lock-free algorithms in the Java language.” &lt;/i&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Primitive_wrapper_class#Atomic_wrapper_classes&quot;&gt;Wikipedia article on Atomic Wrapper Classes in Java&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;read-write-isolation&quot;&gt;Read-Write Isolation&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Readers–writer_lock&quot;&gt;Wikipedia article on Readers-writer lock&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;optimistic-concurrency-and-some-java-data-structures&quot;&gt;Optimistic Concurrency and some Java data structures&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Optimistic_concurrency_control&quot;&gt;Wikipedia article on Optimistic concurrency control&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;java.util.concurrent.*
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicReference.html&quot;&gt;AtomicReference class&lt;/a&gt;, &lt;a href=&quot;https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html&quot;&gt;ConcurrentLinkedQueue class&lt;/a&gt;, &lt;a href=&quot;https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html&quot;&gt;ConcurrentHashMap class&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;linearizability&quot;&gt;Linearizability&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Linearizability&quot;&gt;Wikipedia article on the Linearizability&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;miscellanious&quot;&gt;Miscellanious&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Dining_philosophers_problem&quot;&gt;Dining philosophers problem&lt;/a&gt;: an example problem often used in concurrent algorithm design to illustrate synchronization issues and techniques for resolving them&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Thu, 17 Jan 2019 00:00:00 +0000</pubDate>
        <link>https://deeptanshumalik.com/2019/01/17/concurrent-programming-reading-list/</link>
        <guid isPermaLink="true">https://deeptanshumalik.com/2019/01/17/concurrent-programming-reading-list/</guid>
      </item>
    
  </channel>
</rss>
