Friday, November 18, 2011

Threading Concepts



Why do we use Threading?
Threading enables for multiple processing so that you can do more than one operation at the same point of time.

How to create a new Thread?
You create a new thread by declaring a variable of type Thread and calling the constructor, providing the name of the procedure or method that you want to execute on the new thread.
System.Threading.Thread newTh = new Thread(myMethod);

Does background thread keeps running even after last foreground thread has stopped.
No, background thread stops as soon as the last foreground thread has stopped.


If you want to call a method of a control from another thread than the thread it was created from, what must you use?
Ideally methods of controls should be called only on threads in which they were created, but use Invoke in cases where required otherwise.

Why should we not use lock to lock threads that manipulate forms or controls?
Forms and control methods call back to the calling procedure, thus it will create a deadlock.

How to start Async Operation of Background Worker?
Use the RunWorkerAsync() which calls in the DoWork() event of background worker.
Use CancelAsync() to cancel the Async operation.

How do you return values from Multithreaded procedures?
Initialize the BackgroundWorker. Inside DoWork get the argument of object from e.Argument and then call in the calculation method and assign the value to e.Result.Get the value back into use in RunWorkerCompleted event.

What does lock() statement do?
It can be used to ensure that a block of code runs to completion without interruptions from other threads. i.e. getting mutually exclusive lock for a given object for duration of the code block.

What does argument in lock() statement signify?
It gives the scope of the lock. Its sole purpose is to identify the resources being shared among multiple threads.
Thus don’t use lock(this) if the instance can be accessed publicly. Avoid locking on the public data types. Also avoid putting lock on string litterals as they are interned by CLR i.e. a single instance across application. Its recommended to put lock in protected or private members.

What are Monitors?
They also prevent from simultaneous execution of block of code.
Adv.
Enter method allows one and only one thread to enter, Exit releases the rights.
Object obj=new Object()
Threading.Monitor.Enter(obj);
DoSomething();
Threading.Monitor.Exit(obj);

Which is preferred lock or Monitor?
Lock is preffered as the lock is released implicitly in Monitor we use finally block to ensure release of the lock.

What is WaitOne(), WaitAll(), WaitAny()?
WaitHandle.WaitOne() causes the thread to wait until a single event becomes signaled, WaitHandle.WaitAny() blocks a thread until one or more indicated events become signaled, and WaitHandle.WaitAll() blocks the thread until all of the indicated events become signaled.
 
What is Mutex?
Mutex are similar to Monitor but can also be used to synchronize threads across the processes.

What is Deadlock?
When two threads are trying to lock a resource where each other has already locked. Thus both go into waiting and cannot make any further progress

 What is race conditions?
Race conditions occurs when the outcome of the program depends on which of the two or more thread reaches the code block first. Thus running a program may return different results most of the times and outcome cannot be predicted

In case of Multiple Threads to enable synchronization what should be used?
Don’t use Thread.Suspend() and Thread.Resume() to synchronize. Use Mutex, Monitor in these cases.

Should we control the execution of the worker thread from main program.
No.

Should we have lock on Type objects.
Never take lock on the Types. i.e. lock(typeof(x)) as for the given type there can be only one instance per application domain.

Where should Exit() of monitor be used?
Call the Exit() always in the finally block which ensures that the lock is released. If you cannot ensure that finally will be called use Mutex instead.

Where would multiple threads be used?
Use multiple threads when threads would be performing tasks on different resources. For e.g, a background thread can be used to do I/O operations where other thread to do the network connection operation.

How can performance from lock be increased?
Use Increment method in place of lock for increased performance.

What are foreground threads and background threads?
All the threads which does not keep the managed execution environment running are called as background thread.
Threads that belong to the managed thread pool  i.e with IsThreadPoolThread property as true are background threads. All that enter managed execution environment from unmanaged code are background threads.
All threads generated by new Thread object are foreground threads.

What is a thread pool?
Thread pool is a collection of threads that can be used to perform number of tasks in background

1 comment:

navya said...

Threading Concepts involves You create a new thread by declaring a variable of type Thread and calling the constructor, providing the name of the procedure or method that you want to execute on the new thread.
In C# Training we have to learn multithreading to execute methods with synchronization.