惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
S
Secure Thoughts
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
TaoSecurity Blog
TaoSecurity Blog
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
The Last Watchdog
The Last Watchdog
T
Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Know Your Adversary
Know Your Adversary
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
D
Docker
V
Vulnerabilities – Threatpost
Attack and Defense Labs
Attack and Defense Labs
Hugging Face - Blog
Hugging Face - Blog
W
WeLiveSecurity
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Cloudflare Blog
博客园_首页
P
Privacy International News Feed
Scott Helme
Scott Helme
N
News and Events Feed by Topic
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
T
Tenable Blog
H
Hacker News: Front Page
N
News and Events Feed by Topic
罗磊的独立博客
Google Online Security Blog
Google Online Security Blog
S
Security @ Cisco Blogs
Hacker News - Newest:
Hacker News - Newest: "LLM"
A
About on SuperTechFans
有赞技术团队
有赞技术团队
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
G
GRAHAM CLULEY
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - qufo

Golang 线程池 Docker 实现的 redis 主从 Golang 之 Base62 编码 Golang 之 Qrcode 二维码 Golang 之 key-value LevelDB 无废话 Thrift 之 Hello World( PHP 版). [转] putty 使用密钥登陆 OpenSSH 一个视图引发的血案 破一个行业ERP的感想 Practical Ext JS Projects with Gears中关于Gears描述。 七七前一天,搞定 PHP5 + Oracle 8.1.7 去掉PowerDesigner 15 在 Visual Studio 2008里的不兼容。 回家 庆祝自己通过驾驶员考试 2008.08.08.一个有记住意义的时刻。 adverbux.com 明天路考 blank.security 距离2008年8月8日还有8天。
随时10个可用线程--自己涂鸦的“线程池”
qufo · 2008-07-22 · via 博客园 - qufo

为了设计一个自己的“线程池”我给自己做了这么个东西。

这段程序能产生起始的10个线程,这10个线程启动后,每结束一个线程就会有另外一个线程补充进去,这样始终保持有10个线程在活动。

代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;

namespace ThreadPool
{
    
public class Program
    
{
        ArrayList alThread 
= new ArrayList();

        
static void Main(string[] args)
        
{
            Program p 
= new Program();
            p.threadtest();
        }


        
public void threadtest()
        
{
            
for (int i = 0; i < 10; i++)
            
{
                threadok to 
= new threadok();
                to.threadid 
= i;
                to.finished 
= new threadok.Finished(FinishOne);
                ThreadStart starter 
= new ThreadStart(to.run);
                Thread thread 
= new Thread(starter);
                alThread.Add(to);
                thread.Start();

            }

            Console.WriteLine(
"OK!");
            Console.ReadLine();
        }



        
public void FinishOne(threadok to)
        
{
            
//
            Console.WriteLine("ThreadID:{0},Finished", to.threadid.ToString());
            alThread.Remove(to);

            
if (alThread.Count < 10)
            
{
                Random rnd 
= new Random();

                threadok toa 
= new threadok();
                toa.threadid 
= rnd.Next(100);
                toa.finished 
= new threadok.Finished(FinishOne);
                ThreadStart starter 
= new ThreadStart(toa.run);
                Thread thread 
= new Thread(starter);
                alThread.Add(toa);
                thread.Start();
            }

        }

    }


    
public class threadok
    
{
        
public delegate void Finished(threadok to);
        
public Finished finished;

        
public int threadid;

        
public void run()
        
{
            Console.WriteLine(
"ThreadID:{0}", threadid.ToString());
            Thread.Sleep(
1000);
            finished(
this);
        }


    }

}


最后,这个所谓的“线程池”跟正宗的“线程池”不可同日而语。