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

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

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


    }

}


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