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

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - buru

django begining Update 两个表之间更新数据问题 用户控件与应用页面的事件顺序 linq小问题总结 “尝试读取或写入受保护的内存”错误处理 firefox浏览器的默认事件 配置rails运行环境 通过c#和ironruby学习 ruby语言 SQL 语句查询与性能 操作Access数据库碰到的几个问题 asp.net的CodeDom zz天涯-关于职业生涯的 zz两个存储过程 看《C#线程参考手册》 Mediator 中介者模式 让firefox支持IE的一些属性 IE与firefox取得事件对象的函数(zz) proxy模式 c#通过url获取文件
一个线程管理器
buru · 2007-11-21 · via 博客园 - buru

using System;
using System.Text;
using System.Threading;
using System.Collections;

namespace GenThreadPool
{
    
public interface IThreadPool
    
{
        
void AddJob(System.Threading.Thread jobToRun);
        Stats GetStats();
          
        
    }


    
public class GenPool
    
{
        
private Object _lock;
        
private GenThreadPoolImpl _gn;
        
public GenPool(Object lock_,GenThreadPoolImpl gn)
        
{
         
this._lock=lock_;
         
this._gn = gn;
        }

        
public void Run()
        
{
            Thread job 
= null;
            
try
            
{
                
while (true)
                
{
                    
while (true)
                    
{
                        
lock (this._lock)
                        
{
                            
if (_gn.PendingJobs.Count == 0)
                            
{
                                
int index = _gn.FindThread();
                                
if (index == -1return;
                                ((ThreadElement)_gn.AvailableThreads[index]).Idle 
= true;
                                
break;
                            }

                            job 
= (Thread)_gn.PendingJobs[0];
                            _gn.PendingJobs.RemoveAt(
0);
                        }
//end lock
                        job.Start();
                    }

                    
try
                    
{
                        
lock (this)
                        
{
                            
if (_gn.MaxIdleTime == -1)
                                Monitor.Wait(
this);
                            
else Monitor.Wait(this, _gn.MaxIdleTime);
                        }

                    }

                    
catch { }
                    
lock (_lock)
                    
{
                        
if (_gn.PendingJobs.Count == 0)
                        
{
                            
if (_gn.MinThreads != -1 && _gn.AvailableThreads.Count > _gn.MinThreads)
                            
{
                                _gn.RemoveThread();
                                
return;
                            }

                        }

                    }

                }

            }

            
catch { }
        }

    }

   
    
public class GenThreadPoolImpl : IThreadPool
    
{
        
public GenThreadPoolImpl()
        
{
            _maxThreads 
= 1;
            _minThreads 
= 0;
            _maxIdleTime 
= 300;
            
this._pendingJobs = ArrayList.Synchronized(new ArrayList());
            
this._availableThreads = ArrayList.Synchronized(new ArrayList());
            _debug 
= false;
        }

        
public GenThreadPoolImpl(int maxThreads,int minThreads,int maxIdleTime)
        
{
            _maxThreads 
= maxThreads;
            _minThreads 
= minThreads;
            _maxIdleTime 
= maxIdleTime;
            
this._pendingJobs = ArrayList.Synchronized(new ArrayList());
            
this._availableThreads = ArrayList.Synchronized(new ArrayList());
            _debug 
= false;
            InitAvailableThreads();
        
        }


        
private void InitAvailableThreads()
        
{
            
if (this._minThreads > 0)
            
{
                
for (int i = 0; i < this._minThreads; i++)
                

                  Thread t
=new Thread(new ThreadStart(new GenPool(this,this).Run));
                  ThreadElement e 
= new ThreadElement(t);
                  e.Idle 
= true;
                  _availableThreads.Add(e);
                }

                Console.WriteLine(
"Initialized the ThreadPool," + "Number of Available threads:" + this._availableThreads.Count);
            }

        }

        
private int _maxThreads;
        
private int _minThreads;

        
public int MinThreads1
        
{
            
get return _minThreads; }
            
set { _minThreads = value; }
        }


        
public int MinThreads
        
{
            
get return _minThreads; }
            
set { _minThreads = value; }
        }

        
private int _maxIdleTime;

        
public int MaxIdleTime
        
{
            
get return _maxIdleTime; }
            
set { _maxIdleTime = value; }
        }

        
private static bool _debug;

        
public static bool Debug
        
{
            
get return GenThreadPoolImpl._debug; }
            
set { GenThreadPoolImpl._debug = value; }
        }

        
private ArrayList _pendingJobs;

        
public ArrayList PendingJobs
        
{
            
get return _pendingJobs; }
            
set { _pendingJobs = value; }
        }

        
private ArrayList _availableThreads;

        
public ArrayList AvailableThreads
        
{
            
get return _availableThreads; }
            
set { _availableThreads = value; }
        }


        
IThreadPool 成员



         
public  int FindThread()
        
{
            
for (int i = 0; i < _availableThreads.Count; i++)
            
{
                
if (((ThreadElement)_availableThreads[i]).GetMyThread().Equals(Thread.CurrentThread))
                
{
                    
return i;
                }

            }

            
return -1;
        }

         
public void RemoveThread()
         
{
             
for (int i = 0; i < _availableThreads.Count; i++)
             
{
                 
if (((ThreadElement)_availableThreads[i]).GetMyThread().Equals(Thread.CurrentThread))
                 
{
                     _availableThreads.RemoveAt(i);
                     
return;
                 }

             }
   
          }

    }

       
    
public struct Stats
    
{
        
public int maxThreads;
        
public int minThreads;
        
public int maxIdleTime;
        
public int numThreads;
        
public int pendingJobs;
        
public int jobsInProgress;

        
public override string ToString()
        
{
            StringBuilder sb 
= new StringBuilder("MaxThread=");
            sb.Append(maxThreads);
            sb.Append(
"\nMinThreads=");
            sb.Append(minThreads);
            sb.Append(
"\nMaxThreads=");
            sb.Append(maxThreads);
            sb.Append(
"\npendingJobs=");
            sb.Append(pendingJobs);
            sb.Append(
"\nJobs In Progress=");
            sb.Append(jobsInProgress);

            
return sb.ToString();
                        
        }

    }

     
     
public class ThreadElement
     
{
         
private bool _idle;

         
public bool Idle
         
{
             
get return _idle; }
             
set { _idle = value; }
         }


        
         
private Thread _thread;
         
public ThreadElement(Thread th)
         
{
            _thread
=th;
             
this._idle=true;
         }

         
public Thread GetMyThread()
         
{
             
return this._thread;
         }

     }

}