2012-09-05 33 views
1

我使用以下.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計算。

事實上,當線程稍後執行時,打印耗時的步驟會先打印出來。

+0

目前還不清楚,你想要計算什麼。 – Dennis

+0

我正在計算運行所有線程所耗費的時間 –

回答

1

只要你想你的分也不會做 - 這裏有2個問題

  1. 有一個Console.Readline這也將等待用戶的內部秒錶
  2. 您還沒有同步你的線程輸入正確 - 幸運的是,線程通常在等待用戶輸入的同時完成,但這不會讓您爲線程定時。

以下內容應通過將秒錶緊緊圍繞線程移動並提供CountDownEvent來允許每個線程指示何時完成。

Shared Sub RunMonitor() 
     Dim stopwatch As New Stopwatch() 
     stopwatch.Start() 
     Dim o As New Object() 
     Const numThreads As Integer = 10 
     ' Create an Event which signals when we are finished 
     Dim allDoneEvent As New CountdownEvent(numThreads) 
     For i As Integer = 0 To numThreads - 1 
      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) 
               ' This thread indicates that it has finished its work 
               allDoneEvent.Signal() 
              End Try 
              Return Nothing 
             End Function 
     ) 
     Next 
     ' Wait for all events to finish, or after a timeout 
     allDoneEvent.Wait(10000) 
     ' Stop the stopwatch before the readline 
     stopwatch.Stop() 
     Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed) 
     Console.ReadLine() 
    End Sub 
+0

爲什麼從終端調用Signal以及如果代碼最終沒有嘗試捕獲該怎麼辦,我必須在Monitor,Mutex,ReaderWriterLock,Semaphore等類似程序中放置類似的計數器,而不是所有代碼雖然ThreadPool.Queue的概念是相同的,並且所有代碼示例的線程數量相同,也就是最終有相同的代碼塊100 –

+0

@AbhishekGupta您不一定需要最終放置信號,但它通常是最好的地方,因爲我們希望確保我們的線程在代碼的好或不好退出時標記爲完整。我還建議你等待一段時間,例如'allDoneEvent.Wait(10000)' – StuartLC