Thread Pool : ThreadPool « Thread « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Thread » ThreadPool 
23.9.1.Thread Pool
Imports System.Threading

Module Module1

    Sub TaskA(ByVal state As Object)
        Dim As Integer

        For I = To 250
            System.Console.Write("A")
        Next

        Thread.Sleep(3000)

        CType(state, AutoResetEvent).Set()
    End Sub

    Sub TaskB(ByVal state As Object)
        Dim As Integer

        For I = To 250
            System.Console.Write("B")
        Next

        Thread.Sleep(3000)

        CType(state, AutoResetEvent).Set()
    End Sub

    Sub TaskC(ByVal state As Object)
        Dim As Integer

        For I = 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
23.9.ThreadPool
23.9.1.Thread Pool
23.9.2.Get Available Threads in a ThreadPool and Get Max Threads in a ThreadPool
23.9.3.Queue a simple task, represented by the ThreadProc method, using the QueueUserWorkItem method.
23.9.4.Use the QueueUserWorkItem method to queue a task and supply the data for the task.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.