|
|
| 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). To interrupt this sleep you can call the Thread.Interrupt method.
|
| What is Suspend and Resume in Threading? |
It is similar to Sleep and Interrupt. Suspend allows you to block a thread until another thread calls Thread.Resume. 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.
|
| What the way to stop a long running thread? |
Thread.Abort() stops the thread execution at that moment itself.
|
| What is Thread.Join() in threading? |
|
|
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 starting another task. The Join method waits a specified amount of time for a thread to end. If the thread ends before the time-out, Join returns true; otherwise it returns False. Once you call 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 execution and then again invoke "Thread1".
Thread.Join(Integer) ensures that threads do not wait for a long time. If it exceeds a specific time which is provided in integer the waiting thread will start.
|
|
| What are Daemon threads and how can a thread be created as Daemon? |
Daemon thread's run in background and stop automatically when nothing is running program. Example of a Daemon thread is "Garbage collector". Garbage collector runs until some .NET code is running or else its idle.
You can make a thread Daemon by
Thread.Isbackground = true
|
| When working with shared data in threading how do you implement synchronization? |
There are certain situations that 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 will have a problem. This can be very difficult to debug because they may not always do it at exactly the same time. To avoid the problem, you can can lock a variable before accessing it. However, if the two threads lock the same variable at the same time, you will have a deadlock problem.
|
| Can we use events with threading? |
Yes, you can use events with thread; this is one of the techniques to synchronize one thread with other.
|
| 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.
|
| What is use of Interlocked class? |
|
Interlocked class provides methods by whcih you can acheive 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)
|
|
|
| What is a monitor object? |
Monitor objects are used to ensure that a block of code runs without being interrupted by code running on other threads. In other words, code in other threads cannot run until code in the synchronized code block has finished.
SyncLock and End SyncLock statements are provided in order to simplify access to monitor object.
|
|
|
|
|