*
Previous C# Question Answers-111 - 120 C# Threading QA-11-20 Next

Dot net Framework (C#) Threading Question Answers - 1- 10

1. AutoResetEvent Inheritable?

No, AutoResetEvent class cannot be inherited it’s a sealed Class.

2. Can we change the thread's name?

No, You can set a thread’s name just once; attempts to change it later will throw an exception.

3. Can we implement our own Thread pool in C#?

Yes, we can implement our own thread pool.

4. Can we set Name to a pooled Thread?

No, You cannot set the Name of a pooled thread.

5. Can we set the name to thread?

Yes, Each thread has a Name property that we can set. This name is displayed in the Threads Window and Debug Location toolbar

6. Can we use await inside lock block?

No, You can't use the await keyword in the body of a lock statement.

7. ConcurrentBag allows duplicate entry?

Yes, ConcurrentBag support duplicates.

8. Difference between STA and MTA

Difference between STA (Single Threaded Apartment) and MTA (Multi Threaded Apartment)

STA:
1. In STA, there may be multiple apartments, but only single thread can be executed in a apartment.
2. In STA, if there is a need to communicate between threads we need a proxy, they can not communicate directly.
3. If the COM object cannot handle its own synchronization i.e. not thread safe then the STA model can be used
4. Marshal all pointers to objects when passing them between apartments.
Every object should live on only one thread (within a single-threaded apartment).
Single-threaded apartments without objects (client only) also need a message loop to dispatch the broadcast messages that some applications use.

MTA:
1.In MTA, only one apartment will be there and all threads will execute within that single apartment.
2.In MTA, threads communicate directly to each other without a proxy.
3.If the COM object can handle its own synchronization then the MTA model can be used
4. in MTA There is no need to marshal between threads.
COM provides call synchronization for single-threaded apartments only.
Multithreaded apartments do not receive calls while making calls (on the same thread).
Multithreaded apartments cannot make input-synchronized calls.
Asynchronous calls are converted to synchronous calls in multithreaded apartments.
The message filter is not called for any thread in a multithreaded apartment.

9. Difference between Thread.Sleep(0) and Thread.Yield()?

Thread.Sleep() relinquishes the thread’s current time slice immediately, voluntarily handing over the CPU to other threads.
Thread.Yield() method does the same thing—except that it relinquishes only to threads running on the same processor.

10. Explain how to achieve Thread Synchronization?

For simple operations on integral numeric data types, synchronizing threads can be accomplished with members of the Interlocked class.

For all other data types and non thread-safe resources, multithreading can only be safely performed using the below constructs.

lock: The C# lock statement can be used to ensure that a block of code runs to completion without interruption by other threads. This is accomplished by obtaining a mutual-exclusion lock for a given object for the duration of the code block.

public void Process()
{
    lock (lockThis)
    {
        // Access thread-sensitive resources.
    }
}
Monitors: monitors prevent blocks of code from simultaneous execution by multiple threads. The Enter method allows one and only one thread to proceed into the following statements; all other threads are blocked until the executing thread calls Exit. For example:
System.Object obj = (System.Object)x;
System.Threading.Monitor.Enter(obj);
try
{
    DoSomething();
}
finally
{
    System.Threading.Monitor.Exit(obj);
}
Mutex Object: A mutex is similar to a monitor; it prevents the simultaneous execution of a block of code by more than one thread at a time. In fact, the name "mutex" is a shortened form of the term "mutually exclusive." Unlike monitors, however, a mutex can be used to synchronize threads across processes. A mutex is represented by the Mutex class.

ReaderWriter Locks: The ReaderWriterLock class enforces exclusive access to a resource while a thread is modifying the resource, but it allows non-exclusive access when reading the resource. ReaderWriter locks are a useful alternative to exclusive locks, which cause other threads to wait, even when those threads do not need to update data.

Back to Index
Previous C# Question Answers-111 - 120 C# Threading QA-11-20 Next
*
*