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

推荐订阅源

B
Blog RSS Feed
L
LangChain Blog
博客园_首页
量子位
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
H
Help Net Security
T
Threatpost
N
Netflix TechBlog - Medium
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
C
Cisco Blogs
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
U
Unit 42
博客园 - 三生石上(FineUI控件)
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CXSECURITY Database RSS Feed - CXSecurity.com
Security Latest
Security Latest
WordPress大学
WordPress大学
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
C
Check Point Blog
TaoSecurity Blog
TaoSecurity Blog
Project Zero
Project Zero
www.infosecurity-magazine.com
www.infosecurity-magazine.com
SecWiki News
SecWiki News
F
Full Disclosure
S
Security @ Cisco Blogs
T
Tor Project blog
V
V2EX
Y
Y Combinator Blog
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
B
Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

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


    }

}


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