VB.Net program to create a thread pool

Here, we are going to learn how to create a thread pool in VB.Net?
Submitted by Nidhi, on January 04, 2021

Here, we will use Thread class and import the System.Threading namespace, and create a class that contains two functions and bind the functions using ThreadPool class.

Program/Source Code:

The source code to create a thread pool is given below. The given program is compiled and executed successfully.

'VB.net program to create a thread pool.

Imports System.Threading

Module Module1
    Class ThreadEx
        Public Sub MyThreadFun1()
            For i = 1 To 5 Step 1
                Console.WriteLine("Thread1 Executed")
            Next
        End Sub
        Public Sub MyThreadFun2()
            For i = 1 To 5 Step 1
                Console.WriteLine("Thread2 Executed")
            Next
        End Sub
    End Class

    Sub Main()
        Dim T As New ThreadEx()

        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf T.MyThreadFun1))
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf T.MyThreadFun2))
    End Sub
End Module

Output:

Thread1 Executed
Thread1 Executed
Thread1 Executed
Thread1 Executed
Thread1 Executed
Thread2 Executed
Thread2 Executed
Thread2 Executed
Thread2 Executed
Thread2 Executed
Press any key to continue . . .

Explanation:

In the above program, we imported the System.Threading namespace to implement multithreading in the program. After that, we created a module Module1. Module1 contains a class ThreadEx that contains two functions. Then we created a Main() function.

The Main() function is the entry point for the program, And, we created the object of ThreadEx class and then add the functions in the thread pool using QuickeUserWorkItem() method of ThreadPool class.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.