*
Previous C# Question Answers-141 - 152 C#/.NET Core Coding Guidelines Next

Dot net Framework (C#) Threading Question Answers - 51-62

51. What is the thread state when calling Sleep?

WaitSleepJoin state.

52. What is thread affinity?

Thread Affinity means that only the thread that instantiates them can subsequently access their members. Violating this causes either unpredictable behavior, or an exception to be thrown.

The thread that creates a WPF UI element owns the elements and other threads can not interact with the UI elements directly,this is known as thread affinity.

53. What is Thread Join?

Join blocks the calling thread until the target thread terminates.

54. What is Thread pool?

A thread pool is a collection of threads that can be used to perform a number of tasks in the background. This leaves the primary thread free to perform other tasks asynchronously.

Thread pools are often employed in server applications. Each incoming request is assigned to a thread from the thread pool, so the request can be processed asynchronously, without tying up the primary thread or delaying the processing of subsequent requests.

Once a thread in the pool completes its task, it is returned to a queue of waiting threads, where it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task.

Thread pools typically have a maximum number of threads. If all the threads are busy, additional tasks are placed in queue until they can be serviced as threads become available.

You can implement your own thread pool, but it is easier to use the thread pool provided by the .NET Framework through the ThreadPool class.

55. What is TPL?

The Task Parallel Library (TPL)

The Task Parallel Library (TPL) is a powerful framework in .NET designed to simplify writing multithreaded and parallel code. It’s part of the System.Threading and System.Threading.Tasks namespaces and was introduced in .NET Framework 4 to help developers efficiently utilize multicore processors.

⚙️ What TPL Does

  • Abstracts Thread Management: You don’t need to manually create or manage threads.
  • Uses ThreadPool Efficiently: Tasks are scheduled on the .NET ThreadPool, optimizing performance.
  • Supports Data & Task Parallelism: Includes constructs like Parallel.For, Parallel.ForEach, and Task.Run.
  • Handles Cancellation & Continuations: Built-in support for CancellationToken, Task.WhenAll, Task.ContinueWith, etc.
  • Scales Automatically: Dynamically adjusts concurrency based on available CPU cores.

56. What is UpgradeToWriterLock?

Upgrades a reader lock to the writer lock, using an Int32 value for the time-out.

When a thread calls UpgradeToWriterLock the reader lock is released, regardless of the lock count, and the thread goes to the end of the queue for the writer lock. Thus, other threads might write to the resource before the thread that requested the upgrade is granted the writer lock.

When a thread has no reader lock, do not use UpgradeToWriterLock. Use AcquireWriterLock instead.

57. When does a thread state become Running?

Another thread calls the Thread.Start()  method on the new thread, and the call returns. The Start method does not return until the new thread has started running. There is no way to know at what point the new thread will start running, during the call to Start.

58. When does a thread state become Stopped?

When it finishes execution or is aborted.

59. When to use Asynchronous delegates?

Use when background work must be done and you need results when it finishes.

60. When to use BackgroundWorker / When not?

Use BackgroundWorker: If you have a single task that runs in the background and needs to interact with the UI. and use it if you don't care when they finish their task. The task of marshalling data and method calls to the UI thread are handled automatically through its event-based model.

Avoid BackgroundWorker:
(1) Your assembly does not already reference the System.Windows.Form assembly
(2) You need the thread to be a foreground thread, or
(3) you need to manipulate the thread priority.
Use: For a single background task that interacts with the UI.
Avoid: If you don’t reference System.Windows.Forms, need foreground threads, or need priority control.

61. Which are thread-safe and scalable collections?

  • BlockingCollection<T> : Provides bounding and blocking functionality for any type that implements IProducerConsumerCollection<T>. For more information, see BlockingCollection Overview.
  • ConcurrentDictionary<TKey,TValue>: Thread-safe implementation of a dictionary of key-value pairs.
  • ConcurrentQueue<T>: Thread-safe implementation of a FIFO (first-in, first-out) queue.
  • ConcurrentStack<T>: Thread-safe implementation of a LIFO (last-in, first-out) stack.
  • ConcurrentBag<T>: Thread-safe implementation of an unordered collection of elements.
  • IProducerConsumerCollection<T>: The interface that a type must implement to be used in a BlockingCollection.

62. Which exception is thrown when calling Abort?

It Raises a ThreadAbortException. in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.

Back to Index
Previous C# Question Answers-141 - 152 C#/.NET Core Coding Guidelines Next
*
*