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

推荐订阅源

Cyberwarzone
Cyberwarzone
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Exploit Database - CXSecurity.com
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
A
Arctic Wolf
Simon Willison's Weblog
Simon Willison's Weblog
美团技术团队
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
NISL@THU
NISL@THU
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
N
News and Events Feed by Topic
M
MIT News - Artificial intelligence
T
Tenable Blog
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
S
Security Affairs
T
Troy Hunt's Blog
月光博客
月光博客
SecWiki News
SecWiki News
PCI Perspectives
PCI Perspectives
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
IT之家
IT之家
F
Full Disclosure
博客园_首页
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Last Week in AI
Last Week in AI
C
Cisco Blogs
H
Heimdal Security Blog
大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Attack and Defense Labs
Attack and Defense Labs
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
量子位
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
I
InfoQ
P
Proofpoint News Feed
Y
Y Combinator Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main

博客园 - 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);
        }


    }

}


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