Frequently Asked Interview Questions

Threading

You can also place a thread into the sleep state for an indeterminate amount of time bycalling Thread.Sleep (System.Threading.Timeout.Infinite).To interrupt this sleep you cancall the Thread.Interrupt method.
It is Similar to Sleep and Interrupt. Suspend allows you to block a thread until anotherthread calls Thread.Resume. The difference between Sleep and Suspend is that the latterdoes not immediately place a thread in the wait state. The thread does not suspend untilthe .NET runtime determines that it is in a safe place to suspend it. Sleep will immediatelyplace a thread in a wait state.
Thread.Abort() stops the thread execution at that moment itself.
There are two versions of Thread.Join :-

√ Thread.join().
√ Thread.join(Integer) this returns a boolean value.

The Thread.Join method is useful for determining if a thread has completed before startinganother task. The Join method waits a specified amount of time for a thread to end. If thethread ends before the time-out, Join returns True; otherwise it returns False.Once youcall Join the calling procedure stops and waits for the thread to signal that it is done.

Example you have "Thread1" and "Thread2" and while executing 'Thread1" you call"Thread2.Join()".So "Thread1" will wait until "Thread2" has completed its executionand the again invoke "Thread1".

Thread.Join(Integer) ensures that threads do not wait for a long time.If it exceeds a specifictime which is provided in integer the waiting thread will start.
Daemon thread's run in background and stop automatically when nothing is runningprogram.Example of a Daemon thread is "Garbage collector".Garbage collector runsuntil some .NET code is running or else its idle.

You can make a thread Daemon by

Thread.Isbackground=true
There are a somethings you need to be careful with when using threads. If two threads(e.g. the main and any worker threads) try to access the same variable at the same time,you'll have a problem. This can be very difficult to debug because they may not always doit at exactly the same time. To avoid the problem, you can lock a variable before accessingit. However, if two threads lock the same variable at the same time, you'll have a deadlockproblem.

SyncLock x

'Do something with x

End SyncLock
Yes you can use events with threads , this is one of the technique to synchronize onethread with other.
"ThreadState" property can be used to get detail of a thread.Thread can have one orcombination of status.System.Threading.Threadstate enumeration has all the values todetect a state of thread.Some sample states are Isrunning,IsAlive,suspended etc.
Interlocked class provides methods by which you can achieve following functionalities :-

√ increment Values.
√ Decrement values.
√ Exchange values between variables.
√ Compare values from any thread.
in a synchronization mode.

Example :- System.Threading.Interlocked.Increment(IntA)
Monitor objects are used to ensure that a block of code runs without being interrupted bycode running on other threads. In other words, code in other threads cannot run untilcode in the synchronized code block has finished.

SyncLock and End SyncLock statements are provided in order to simplify access to monitorobject.

Most Visited Pages

Home | Site Index | Contact Us