I am using Following .NET Code
Class MonitorSample
Shared Sub RunMonitor()
Dim o As New Object()
For i As Integer = 0 To 99
ThreadPool.QueueUserWorkItem(Function()
Try
Monitor.Enter(o)
Console.WriteLine("Thread {0} acquired lock...working", Thread.CurrentThread.ManagedThreadId)
Console.WriteLine("Thread {0} performing some I/O operation so yielding the lock temporarily...", Thread.CurrentThread.ManagedThreadId)
Monitor.PulseAll(o)
Monitor.Wait(o)
Console.WriteLine("Thread {0} reacquired lock", Thread.CurrentThread.ManagedThreadId)
Finally
Console.WriteLine("Thread {0} released lock", Thread.CurrentThread.ManagedThreadId)
Monitor.PulseAll(o)
Monitor.Exit(o)
End Try
Return Nothing
End Function
)
Next
Console.ReadLine()
End Sub
Shared Sub Main()
Dim stopwatch As New Stopwatch()
stopwatch.Start()
RunMonitor()
stopwatch.Stop()
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed)
End Sub
End Class
In the main method is this the right method to calculate time consuemd by thread or we should calculate in some otherway.
Actually the step to print time consumed get printed first while threads get executed later.