Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] What's thread Pool, demo thread pool example

Wednesday 22 July 2015
|
Read: Completed in minutes

[Tips] What's thread Pool, demo thread pool example

What's Thread Pool?

A thread pool is a way of managing multiple threads in a computer program. Threads are like mini-programs that can run at the same time and do different tasks. A thread pool creates and reuses a fixed number of threads, instead of making new ones or destroying old ones. This can make the program faster and more efficient, because creating and destroying threads can take a lot of time and resources. A thread pool also lets you control how many threads are running at the same time, and how they handle the tasks they get.

To use a thread pool in C#, you can use the System.Threading.ThreadPool class. This class provides methods to create a thread pool, submit tasks to it, and control its settings. Here are some examples of how to use a thread pool in C#:

• To create a thread pool, you don't need to do anything special. The .NET framework automatically creates a thread pool for each process when it starts.

• To submit a task to the thread pool, you can use the ThreadPool.QueueUserWorkItem method. This method takes a delegate that represents the task you want to execute, and optionally an object that contains some data for the task

Below is detail with another version language

- Bao gồm Các Thread con
- Chạy nhiều Thread khác nhau

What's thread Pool, demo thread pool example in C#



Advantages of Thread Pool


- Thread pooling giúp hạn chế việc máy ảo tạo ra các thread cho các tác vụ nhỏ có thời gian tồn tại ngắn.

- Giảm tối thiểu sự quá tải liên quan tới việc khởi động, thu hồi tài nguyên sau khi các tác vụ kết thúc.

- Bằng việc tạo một Thread Pool, một thread trong đó có thể được thu hồi lại và gán cho tác vụ khác. (Có thể giảm bớt thời gian cho việc thực thi mỗi tác vụ, bởi vì các thread lấy ra từ thread pool bản thân nó đã được khởi tạo và khởi động, ở trạng thái chờ cho việc thực thi tác vụ tiếp theo)

- Cố định kích thước thread pool khi khởi tạo. (việc cố định kích thước thread pool giúp khống chế các tác vụ sẽ được thực thi dựa trên kích thước thread pool)

- Tất cả các thread đã được khởi động và chúng được đặt vào trạng thái đợi (trạng thái sử dụng ít tài nguyên nhất) đến khi một tác vụ được gán cho nó.

Ví dụ trong trường hợp HTTP Server, nhưng thread nằm trong thread pool luôn ở trạng thái sẵn sàng phục vụ cho một yêu cầu mới. Nếu không có thread pool, một thread mới sẽ cần phải được khởi tạo lúc này và phải được khởi động trước khi phục vụ cho các yêu cầu.

How to work:


- Nếu tất cả các thread trong thread pool đã được lấy ra gán cho việc thực thi các tác vụ, thread pool sẽ rỗng, các yêu cầu phục vụ mới sẽ bị đẩy ra hoặc đưa vào trạng thái đợi đến khi có một thread nào đó hoàn thành tác vụ và quay trở lại thread pool.


Ví dụ trong trường hợp HTTP server, nó sẽ ngăn chặn việc lụt các yêu cầu gửi tới từ client gây ra quá tải server khiến server chạy chậm hoặc dừng phục vụ.

Disadvantages of Thread pool


- Thread Pool chỉ phù hợp khi mà các tác vụ có thời gian sống tương đối ngắn. HTTP server là một ví dụ điển hình, các yêu cầu từ phía client đều không diễn ra trong thời gian dài. Bằng cách sử dụng một thread cho mỗi yêu cầu, server có thể phục vụ đồng thời nhiều yêu cầu để đáp ứng nhiều file.

- Nếu một tác vụ chạy vô thời hạn thì một thread bình thường là một lựa chọn tốt hơn là sử dụng thread pool.

- Một sự trả giá khi dùng thread pool là các thread được tạo và sở trạng thái sẵn sàng trong pool theo kỳ vọng và sự phán đoán. Một khả năng có thể xảy ra là dung lượng của thread pool có thể lớn hơn rất nhiều so với cần thiết.

- Một trường hợp khác là thread pool bạn tạo ra có thể quá nhỏ. Các tác vụ sẽ bị từ chối khi pool ở trạng thái rỗng.
  

Using Thread Pool In CSharp: (C#)

Sử dụng QueueUserWorkItem cho thiết lập Thread pool

Source code:

 
          /* *************************************************************
      * Author:          zidane (huuvi168@gmail.com)
      * Last Modified:   20150630
      * *************************************************************/
     
     using System;
     using System.Collections.Generic;
     using System.Text;
     using System.Threading;
     using System.Threading.Tasks;
     
     namespace ThreadPoolDemo
     {
         class clsThread
         {
             public static void run()
             {
                 ThreadPool.QueueUserWorkItem(threadProc);
                 ThreadPool.QueueUserWorkItem(display, 123);
             }
     
             static void display(object data)
             {
                 for (int i = 0; i < 10; i++)
                 {
                     Console.WriteLine("Thread callback: " + data);
                     Thread.Sleep(500);
                 }
             }
         }
     }
     

Result:

What's thread Pool, demo thread pool example


Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about What's thread Pool, demo thread pool example , please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋  Webzone Tech Tips Zidane, all things tech tips web development  - I am Zidane, See you next time soon ✋✋✋✋

🙇🏼 We Appreciate Your Comments and Suggestions - Webzone - all things Tech Tips web development 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com