Threading

Posted by Bhaumik | 6:35 AM

Que:- What is Multi-tasking ?
Ans:- It’s a feature of modern operating systems with which we can run multiple programs at same time example Word, Excel etc.

Que:- What is Multi-threading ?
Ans:- Multi-threading forms subset of Multi-tasking. Instead of having to switch between programs this feature switches between different parts of the same program. Example you are writing in word and at the same time word is doing a spell check in background.

Que:- What is a Thread ?
Ans:- A thread is the basic unit to which the operating system allocates processor time.

Que:- Can we have multiple threads in one App domain ?
Ans:- One or more threads run in an AppDomain. An AppDomain is a runtime representation of a logical process within a physical process. Each AppDomain is started with a single thread, but can create additional threads from any of its threads.
Note :- All threading classes are defined in System.Threading namespace.

Que:- Can you explain in brief how can we implement threading ?
Ans:-

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim pthread1 As New Thread(AddressOf Thread1)
Dim pthread2 As New Thread(AddressOf Thread2)
pthread1.Start()
pthread2.Start()
End Sub
Public Sub Thread1()
Dim pintcount As Integer
Dim pstr As String
pstr = “This is first thread”
Do Until pintcount > 5
lstThreadDisplay.Items.Add(pstr)
pintcount = pintcount + 1
Loop
End Sub
Public Sub Thread2()
Dim pintcount As Integer
Dim pstr As String
pstr = “This is second thread”
Do Until pintcount > 5
lstThreadDisplay.Items.Add(pstr)
pintcount = pintcount + 1
Loop
End Sub

Que:- How can we change priority and what the levels of priority are provided by .NET ?
Ans:- Thread Priority can be changed by using Threadname.Priority = ThreadPriority.Highest.
In the sample provided look out for code where the second thread is ran with a high
priority.
Following are different levels of Priority provided by .NET :-
v ThreadPriority.Highest
v ThreadPriority.AboveNormal
v ThreadPriority.Normal
v ThreadPriority.BelowNormal
v ThreadPriority.Lowest

Que:- What's Thread.Sleep() in threading ?
Ans:- Thread's execution can be paused by calling the Thread.Sleep method. This method takes an integer value that determines how long the thread should sleep. Example Thread.CurrentThread.Sleep(2000).

Que:- What is Suspend and Resume in Threading ?
Ans:- 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.
Note :- In threading interviews most people get confused with Sleep and Suspend. They look very similar.

Que:- How can we know a state of a thread?
Ans:- "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.

Que:- What is use of Interlocked class ?
Ans:- Interlocked class provides methods by which you can achieve following functionalities :-
v Increment Values.
v Decrement values.
v Exchange values between variables.
v Compare values from any thread.
in a synchronization mode.
Example :- System.Threading.Interlocked.Increment(IntA)

Que:- How can you avoid deadlock in threading?
Ans:- A good and careful planning can avoid deadlocks.There are so many ways Microsoft has provided by which you can reduce deadlocks example Monitor, Interlocked classes, Wait handles, Event raising from one thread to other thread, ThreadState property which you can poll and act accordingly etc.

Que:- What is the difference between thread and process?
Ans:- A thread is a path of execution that run on CPU, a process is a collection of threads that share the same virtual memory. A process has at least one thread of execution, and a thread always run in a process context.