zoukankan      html  css  js  c++  java
  • c# 线程池实现 只是一个原理性的实现细节内容忽略

    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;

    namespace ThreadPoolImp
    {
        public class MyThreadExcutor
        {
            //创建
            private static volatile bool RUNNING = true;

            //所有任务都放队列中,让工作线程来消费
            private static ConcurrentQueue<Action> queue = null;

            private static HashSet<Worker> workers = new HashSet<Worker>();

            private static List<Thread> threadList = new List<Thread>();
            //工作线程数
            int poolSize = 0;
            //核心线程数(创建了多少个工作线程)
            int coreSize = 0;

            static bool shutdown = false;
            public MyThreadExcutor(int poolSize)
            {
                this.poolSize = poolSize;
                queue = new ConcurrentQueue<Action>();
            }
            public void Exec(Action action)
            {
                if (action == null) { throw new ArgumentNullException(); }
                if (coreSize < poolSize)
                {
                    addThread(action);
                }
                else
                {
                    try
                    {
                        queue.Enqueue(action);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

            private void addThread(Action action)
            {
                coreSize++;
                Worker worker = new Worker(action);
                workers.Add(worker);
                Thread t = new Thread(worker.Run);
                threadList.Add(t);
                try
                {
                    t.Start();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public void ShutDown()
            {
                RUNNING = false;
                if (workers.Count > 0)
                {
                    foreach (Worker item in workers)
                    {
                        item.InterruptAll();
                    }
                }
                shutdown = true;
                Thread.CurrentThread.Interrupt();
            }
            class Worker
            {
                private readonly static object lockObj = new object();
                public Worker(Action action)
                {
                    queue.Enqueue(action);
                }

                public void Run()
                {
                    while (RUNNING)
                    {
                        if (shutdown==true)
                        {
                            Thread.CurrentThread.Interrupt();
                        }
                        Action task = null;
                        try
                        {
                            task = GetTask();
                            if (task != null)
                            {
                                task();
                            }
                            else
                            {
                                Thread.CurrentThread.Interrupt();
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }

                public Action GetTask()
                {
                    Action result;
                    queue.TryDequeue(out result);
                    return result;
                }
                public void InterruptAll()
                {
                    for (int i = 0; i < threadList.Count; i++)
                    {
                        threadList[i].Interrupt();
                    }
                }
            }

        }
    }

  • 相关阅读:
    VS2005 Web安装程序 创建程序菜单组
    文件夹 文件 加入/去除 Everyone全控
    [转]asp.net 部署数据库、开始菜单、桌面快捷方式实例
    身边的贵人
    AppCode下的cs类 取得相关路径
    遭遇“windows已经阻止此软件因为无法验证发行者”
    成功不是忽悠
    关于 软件注册授权 防止被大面积免费扩散 的设想
    [转]获取机器的硬件信息(CPU ID序列号, 主板信息,硬盘序列号,系统信息)
    递交辞呈之后
  • 原文地址:https://www.cnblogs.com/cppfans140812/p/9638055.html
Copyright © 2011-2022 走看看