Threading Interview Questions

Kailash Chandra Behera | Thursday, May 21, 2020
  1. What is threading

    A threading is an execution path of a method.it is the basic unit to which the operating system allocates processor time.

  2. Which namespace has threading?

    Any .NET application who wants to implement threading has to import ‘Systems.Threading’,it has all the classes related to implement threading.

  3. How many threads are running for a .Net Application?

    The .NET program always has at least two threads running one is the main program and second is the garbage collector. Developers can use more threads depends on the use of the application.

  4. How do you get the reference of current thread?

    "Thread.CurrentThread" refers to the current thread running in the method.CurrentThread is a public static property.

  5. How can we know a state of a thread?

    "ThreadState" property can be used to get detail of a thread. Thread can have one or a combination of status.’System.Threading.Threadstate’ enumeration has all the values to detect a state of thread. Some sample states are Isrunning, IsAlive, suspended etc.

  6. What are the states of threads in c#

    There are 10 types of states of thread, that are...

    1. Unstarted :-The Start() method has not been invoked on the thread.
    2. Running :-The thread has been started and not yet stopped.
    3. Stopped :-The thread has stopped.
    4. StopRequested :-The thread is being requested to stop. This is for internal use only.
    5. Suspended :-The thread has been suspended.
    6. Aborted :-The thread state includes AbortRequested and the thread is now dead, but its state has not yet changed to Stopped.
    7. AbortRequested :-The Abort(Object) method has been invoked on the thread, but the thread has not yet received the pending ThreadAbortException that will attempt to terminate it.
    8. SuspendRequested :-The thread is being requested to suspend.
    9. Background :-The thread is being executed as a background thread, as opposed to a foreground thread. This state is controlled by setting the IsBackground property.
    10. WaitSleepJoin :-The thread is blocked. This could be the result of calling Sleep(Int32) or Join(), of requesting a lock - for example, by calling Enter(Object) or Wait(Object, Int32, Boolean) - or of waiting on a thread synchronization object such as ManualResetEvent.

  7. How do you stop to a thread?

    The execution of a thread can be canceled or stopped any type by calling the Thread.Abort() method, which stops the thread execution at that moment itself.

  8. How do we make a thread sleep or pause a thread?

    Thread's execution can be paused by calling the "Thread.Sleep()" method. This method takes an integer value that determines how long the thread should sleep. Example Thread.CurrentThread.Sleep(2000).

  9. How can we make a thread sleep for infinite period?

    You can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep (System.Threading.Timeout.Infinite).

  10. Can we prioritize the execution of a thread?

    Yes

  11. How can we prioritize the execution of a thread?

    Thread Priority can be changed by using thread's Priority property, see the code below which changes the priority of execution to high.

     Threadname.Priority = ThreadPriority.Highest   
    

  12. What are the different levels of Priority?

    1. Highest :- ThreadPriority.Highest
    2. Normal :-ThreadPriority.Normal
    3. AboveNormal :-ThreadPriority.AboveNormal
    4. BelowNormal :-ThreadPriority.BelowNormal
    5. Lowest :-ThreadPriority.Lowest

  13. What is Suspend and Resume in Threading?

    Suspend allows you to block a thread until another Thread calls Thread.Resume.

  14. What is the difference between Sleep and Suspend?

    The difference between Sleep and Suspend is that the latter does not immediately place a thread in the wait state. The thread does not suspend until the .NET runtime determines that it is in a safe place to suspend it. Sleep will immediately place a thread in a wait state.

  15. What is Thread.Join () in threading?

    Blocks the calling thread until the thread represented by this instance terminates.

  16. Can we use events with threading?

    Yes, this is one of the techniques to synchronize one thread with other.

  17. What we implement multiple thread in a application?

    Yes.more than one thread can be use in an application.

Thanks