我使用以下.NET代碼如何計算所有線程
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
在main方法所用的時間是這樣計算的線程consuemd的時間正確的方法還是應該在一些otherway計算。
事實上,當線程稍後執行時,打印耗時的步驟會先打印出來。
目前還不清楚,你想要計算什麼。 – Dennis
我正在計算運行所有線程所耗費的時間 –