All posts by avijit6399

Multithreading Usage in C# and ASP.Net

 

 

Why Multithreading in C# and ASP.Net

Usage of multithreading in .Net can be very useful for different use cases. 
 
  1. Suppose in your web page there are multiple report that needs to be generated based on different complex queries, stored procedures or functions. If you execute one query at a time and populate the Report in GridViews, it will take longer time to complete. Instead of this if you execute multiple queries in parallel and populate those GridViews then the maximum time it may take up to the time to execute the query that take longest to finish.
  2. Sometimes in your page there might be scenarios where you want to show one particular set of data. But if user wants then they can click a button (Lets say “Show More”) and then more data can be shown. You can optimize this kind of scenario by getting data in advance for “Show More” button and that you can easily do by using threading and storing the data in the page and retrieve that by Javascript when required.
 
So threading could be a game changer for your website if you can effectively use it.
 
In the below example I have shown a simplest threading implementation where two different methods has been called by Threading or Non-Threading way and we can see that how Threading way of implementation is executing faster.




Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

namespace TestWebApplication
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            /*
            Serial Execution
            */
            writeLn("Without Threading");
            writeLn("==========================");
            DateTime dt1 = DateTime.Now;
            writeLn("Started at " + dt1);
            doWork1();
            doWork2();
            DateTime dt2 = DateTime.Now;
            writeLn("Ended at " + dt2);
            writeLn("--------------------------");
            writeLn("Time Taken: " + (dt2 - dt1).Seconds + " Second");
            writeLn("");
            writeLn("");


            /*
            Parallel execusion by using Threads
            */
            writeLn("With Threading");
            writeLn("==========================");
            DateTime dt3 = DateTime.Now;
            writeLn("Started at " + dt3);
            Thread t1 = new Thread(new ThreadStart(doWork1));
            t1.Start();

            Thread t2 = new Thread(new ThreadStart(doWork2));
            t2.Start();

            t1.Join();
            t2.Join();
            DateTime dt4 = DateTime.Now;
            writeLn("Ended at " + dt4);
            writeLn("--------------------------");
            writeLn("Time Taken: " + (dt4- dt3).Seconds + " Second");
        }

        private void doWork1()
        {
            writeLn("First Method Started");
            Thread.Sleep(1000);
            writeLn("First Method Ended");
        }

        private void doWork2()
        {
            writeLn("Second Method Started");
            Thread.Sleep(1000);
            writeLn("Second Method Ended");
        }

        private void writeLn(String msg)
        {
            Response.Write(msg + "
");
        }
    }
}

Output

Without Threading
==========================
Started at 7/15/2016 12:55:52 AM
First Method Started
First Method Ended
Second Method Started
Second Method Ended
Ended at 7/15/2016 12:55:54 AM
--------------------------
Time Taken: 2 Second


With Threading
==========================
Started at 7/15/2016 12:55:54 AM
First Method Started
Second Method Started
First Method Ended
Second Method Ended
Ended at 7/15/2016 12:55:55 AM
--------------------------
Time Taken: 1 Second

For using Threading more optimize way in .Net you can read about Thread Pool and implement that as and when required. Will definitely write in near future on Thread Pool in .Net.

Functional Interface in Java


What is Functional Interface

A Functional Interface can have only one abstract method. Now the question can arise that why we need an interface which can have only one abstract method, the answer is, on every occasion we create an Interface for a purpose and that is to have multiple implementation for it which can be dynamically passed where the Interface reference is used. Functional Interface can also has the same ability in a greater way where we can pass any method which has the signature identical to the single abstract method that is contained by the Functional Interface. Since any method can be passed where the interface is used, so there is no need for creating unnecessary classes which need to implement that interface.

 

 

package com.avijit.blog;

// This is the functional interface which can have
// only one method signature.
@FunctionalInterface
interface IPrint {
    public void print(String data);
}

public class DataPrinter {
    
    // Function that take "Functional Interface" IPrint as one
    // argument and the data that needs to be printed as the other.
    public static void printData(IPrint iPrint, String data){
        iPrint.print(data);
    }
    
    // This is a function that's signature matches with the 
    // Functional Interfaces method's signature
    // public void print(String data);
    public static void printDataWithHi(String data){
        System.out.println("Hi: " + data);
    }
    
    // This is another function that's signature matches with the 
    // Functional Interfaces method's signature
    // public void print(String data);
    public static void printDataWithHello(String data){
        System.out.println("Hello: " + data);
    }
    
    public static void main(String[] args) {
        // Call printData function by using different 
        // function thats uses same signature of the 
        // Functional Interface function.
        
        // By using Lambda Expression
        printData(i -> printDataWithHi(i), "Test");
        // Or
        printData(DataPrinter::printDataWithHi, "Test");
        
        // Using another function which will produce different 
        // results.
        printData(i -> printDataWithHello(i), "Test");
        // Or
        printData(DataPrinter::printDataWithHello, "Test");
    }
}

//Output
Hi: Test
Hi: Test
Hello: Test
Hello: Test