.NET Interview Questions - Threading

1. What is multi-tasking?

It is a feature of Operating Systems which helps to run more than one applications at a time like Internet Explorer, Visual Studio and SQL Server etc.

2. What is multi-threading?

We can say that it is a sub-set of multi-tasking. It allows us to perform more than one operations at a time in a single application. For example: When we type in Microsoft Word at a same that does spelling check also in seperate thread.

Thread is smallest unit of any task in operating system.

3. How to implement threading?

To implemet threading in .NET, we need import System.Threading namespace. There are many classes in this namespace but we need to use Thread class.

It has some important method as below:

  • Abort
  • Equals
  • Finalize
  • Join
  • Resume
  • Sleep
  • Start
  • Suspend

 Sample code for threading in c#:

//Import threading namespance
using System.Threading;

// Simple threading scenario:  Start a static method running on a second thread. 
public class ThreadExample {
    // The ThreadProc method is called when the thread starts. It loops ten times.
    // the rest of its time slice each time, and then ends. 
    public static void ThreadProc() {
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("ThreadProc: {0}", i);
            // Yield the rest of the time slice.
            Thread.Sleep(0);
        }
    }

    public static void Main() {
        Console.WriteLine("Main thread: Start a second thread.");
        // ThreadStart delegate that shows the method to be executed on the thread.
        Thread t = new Thread(new ThreadStart(ThreadProc));

        // Start thread process.
        t.Start();
        // Note that on a uniprocessor machine, the new thread does not get any processor time until the main thread is preempted or yields.
        // Uncomment the Thread.Sleep to see the difference.
        //Thread.Sleep(0); 

        for (int i = 0; i < 4; i++) {
            Console.WriteLine("Main thread: blah blah...");
            Thread.Sleep(0);
        }

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
        t.Join();
        Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
        Console.ReadLine();
    }
}


 

Another exmaple: Sometimes, when without creating any windows service we have to run some scheduler in our website. We can start a new process in a separate thread when application starts.

4. How many types of thread priority we have in .NET?

This is an enumurator in .NET. ThreadPriority options in .NET are Highest, AboveNormal, Normal, BelowNormal and Lowest.

5. What is Thread.Sleep? How to pause a process execution for some time which is being executed by a thread?

It can be done by Thread.Sleep method.

 

//For some specific time
System.Threading.Thread.CurrentThread.Sleep(1000); //miliseconds
//Infinite sleep, not advised.
Thread.CurrentThread.Sleep(System.Threading.Timeout.Infinite); //Infinite

 

8. How to stop a thread?

Using Thread.Abort() function we stop a thread execution;

9. What are Daemon Thread?

Daemon threads run in background and stops automatically when not required. We can make a thread Daemon by setting Thread.IsBackground = True. In .NET Garbage Collector is an example of this.

 

Add comment

Loading