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

主な方法は、これがスレッドごとに消費される時間を計算するための正しい方法です。そうでない場合は、別の方法で計算する必要があります。

実際には、消費された時間を印刷するステップが最初に印刷され、スレッドは後で実行されます。

4

1 に答える 1

1

あなたの潜水艦はあなたが望むようにはしません-ここには2つの問題があります

  1. Console.ReadlineStopWatch内でユーザーの入力を待つものもあります
  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
于 2012-09-05T10:32:17.330 に答える