VB.Net Tutorial/Thread/ThreadPool — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:40, 26 мая 2010
Get Available Threads in a ThreadPool and Get Max Threads in a ThreadPool
Imports System.Threading
public class Test
public Shared Sub Main
Dim msg As String : Dim i As Integer
Dim job As New MyJob()
For i = 1 To 5
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf job.MyTask), i)
Next
Thread.Sleep(2000)
End Sub
End class
Public Class MyJob
Private Msg As String
Dim availWorker As Integer = 0 : Dim maxWorker As Integer = 0
Dim availCPT As Integer = 0 : Dim maxCPT As Integer = 0
Public Sub MyTask(ByVal ThreadStateData As Object)
Console.WriteLine("Performing Task. Data Passed: " & ThreadStateData)
ThreadPool.GetAvailableThreads(availWorker, availCPT)
ThreadPool.GetMaxThreads(maxWorker, maxCPT)
Console.WriteLine("--Available Worker Threads: " & availWorker.ToString())
Console.WriteLine("--Maximum Worker Threads: " & maxWorker.ToString())
Console.WriteLine("--Available Completion Port Threads: " & availCPT.ToString())
Console.WriteLine("--Maximum Completion Port Threads: " & maxCPT)
End Sub
End Class
Performing Task. Data Passed: 1 --Available Worker Threads: 24 --Maximum Worker Threads: 25 --Available Completion Port Threads: 1000 --Maximum Completion Port Threads: 1000 Performing Task. Data Passed: 2 --Available Worker Threads: 24 --Maximum Worker Threads: 25 --Available Completion Port Threads: 1000 --Maximum Completion Port Threads: 1000 Performing Task. Data Passed: 3 --Available Worker Threads: 24 --Maximum Worker Threads: 25 --Available Completion Port Threads: 1000 --Maximum Completion Port Threads: 1000 Performing Task. Data Passed: 4 --Available Worker Threads: 24 --Maximum Worker Threads: 25 --Available Completion Port Threads: 1000 --Maximum Completion Port Threads: 1000 Performing Task. Data Passed: 5 --Available Worker Threads: 24 --Maximum Worker Threads: 25 --Available Completion Port Threads: 1000 --Maximum Completion Port Threads: 1000
Thread Pool
Imports System.Threading
Module Module1
Sub TaskA(ByVal state As Object)
Dim I As Integer
For I = 0 To 250
System.Console.Write("A")
Next
Thread.Sleep(3000)
CType(state, AutoResetEvent).Set()
End Sub
Sub TaskB(ByVal state As Object)
Dim I As Integer
For I = 0 To 250
System.Console.Write("B")
Next
Thread.Sleep(3000)
CType(state, AutoResetEvent).Set()
End Sub
Sub TaskC(ByVal state As Object)
Dim I As Integer
For I = 0 To 250
System.Console.Write("C")
Next
Thread.Sleep(3000)
CType(state, AutoResetEvent).Set()
End Sub
Sub Main()
Dim AIsDone As New AutoResetEvent(False)
Dim BIsDone As New AutoResetEvent(False)
Dim CIsDone As New AutoResetEvent(False)
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf TaskA), AIsDone)
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf TaskB), BIsDone)
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf TaskC), CIsDone)
Dim ThreadCount, ThreadPorts As Integer
ThreadPool.GetAvailableThreads(ThreadCount, ThreadPorts)
Console.WriteLine("Thread pool size {0}", ThreadCount)
Console.WriteLine("Waiting for asynchronous threads to complete.")
AIsDone.WaitOne()
Console.WriteLine("A is done")
BIsDone.WaitOne()
Console.WriteLine("B is done")
CIsDone.WaitOne()
Console.WriteLine("C is done")
End Sub
End Module
Thread pool size 25 Waiting for asynchronous threads to complete. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA is done B is done C is done