Let??™s look at an example using the Monitor class by modifying
the example from the previous section:
Imports System
Imports System.Threading
Public Class EntryPoint
Private Shared theLock As Object = New Object()
Private Shared numberThreads As Integer = 0
Private Shared rnd As Random = New Random()
Private Shared Sub RndThreadFunc()
'Manage thread count and wait for a random amount of time
'between(1 and 12) seconds.
CHAPTER 13 n THREADING 291
Try
Monitor.Enter(theLock)
numberThreads += 1
Finally
Monitor.Exit(theLock)
End Try
Dim time As Integer = rnd.Next(1000, 12000)
Thread.Sleep(time)
Try
Monitor.Enter(theLock)
numberThreads -= 1
Finally
Monitor.Exit(theLock)
End Try
End Sub
Private Shared Sub RptThreadFunc()
Do While True
Dim threadCount As Integer = 0
Try
Monitor.Enter(theLock)
threadCount = numberThreads
Finally
Monitor.Exit(theLock)
End Try
Console.WriteLine("{0} thread(s) alive", threadCount)
Thread.Sleep(1000)
Loop
End Sub
Shared Sub Main()
'Start the reporting threads.
Dim reporter As Thread = _
New Thread(AddressOf EntryPoint.RptThreadFunc)
reporter.IsBackground = True
reporter.Start()
'Start the threads that wait random time.
Dim rndthreads As Thread() = New Thread(49) {}
CHAPTER 13 n THREADING 292
For i As UInteger = 0 To 49
rndthreads(i) = _
New Thread(AddressOf EntryPoint.
Pages:
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473