C# ThreadPool and WaitHandle Sample

50 篇文章 0 订阅

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

namespace ThreadPoolApp
{
    class Program
    {
        static int mycount = 0;
        static void Main(string[] args)
        {
            Persons list = new Persons();
            for (int i = 0; i < 80; i++)
            {
                Person person = new Person()
                {
                    Name = string.Format("M{0}", i.ToString()),
                    Age = 10,
                };
                list.Add(person);
            }

            WaitHandle[] waits = GetWaitHandles(list);
            if (waits == null || waits.Length == 0) return;
            ArrayList myList = GetLists(list, waits);
            if (myList == null) return;
          
            foreach (Persons persons in myList)
            {
                ThreadExecutor.Execute(new WaitCallback(ShowMessage), persons);
            }

            WaitHandle.WaitAll(waits);
            Console.WriteLine(string.Format("这里是主线程在工作:共{0}个对象!",mycount.ToString()));
            Persons handledList = new Persons();
            foreach (Persons persons in myList)
            {
                foreach (Person p in persons)
                {
                    handledList.Add(p);
                }
            }
            foreach (Person p in handledList)
            {
                Console.WriteLine("Test是{0}", p.Text);
            }
        }
       
        static void ShowMessage(object state)
        {
            Persons list = (Persons)state;
            AutoResetEvent are = (AutoResetEvent)list.Wait;
            int n = 0;
            lock (list)
            {
                foreach (Person person in list)
                {
                    person.Text = string.Format("{0}-{1}", person.Name, n.ToString());
                    n++;
                    Console.WriteLine("学生姓名是{0},年龄为{1}", person.Name, person.Age);
                        mycount++;
                }
            }
            are.Set();
        }

        static WaitHandle[] GetWaitHandles(Persons persons)
        {
            ArrayList list = new ArrayList();
            int cnt = persons.Count;
            int arrs = 0;
            if (cnt <= 400)
            {
                arrs = 4;
            }
            else if (cnt > 400 && cnt <= 800)
            {
                arrs = 8;
            }
            else if (cnt > 800 && cnt <= 1600)
            {
                arrs = 16;
            }
            else if (cnt > 1600 && cnt <= 2400)
            {
                arrs = 20;
            }
            else if (cnt > 2400)
            {
                arrs = 24;
            }
            for (int i = 0; i < arrs; i++)
            {
                list.Add(new AutoResetEvent(false));
            }
            if (arrs == 0) return null;
            WaitHandle[] waits = new WaitHandle[list.Count];
            list.CopyTo(waits);
            return  waits;
        }

        static ArrayList GetLists(Persons persons,WaitHandle[] waits)
        {
            ArrayList lst = new ArrayList();
            int arrs = waits.Length;
            int cnt = persons.Count;
            int a=0;
            int b = 0;
            a = cnt / arrs;
            b = cnt % arrs;
            for (int i = 0; i < arrs; i++)
            {
                Persons myPers = new Persons();
                myPers.Wait=waits[i];
                for (int j = a * i; j < a * (i + 1); j++)
                {
                    Person p = new Person();
                    p.Name = string.Format("X{0}", j.ToString());
                    p.Age = 18;
                    myPers.Add(p);
                }
                if (i == arrs - 1)
                {
                    for (int k = 0; k < b; k++)
                    {
                        Person p = new Person();
                        int m = (a * (i+1) + k);
                        p.Name = string.Format("X{0}", m.ToString());
                        p.Age = 18;
                        myPers.Add(p);
                    }
                }
                lst.Add(myPers);
            }
            return lst;
        }
    }

    class Persons : List<Person>
    {
        public WaitHandle Wait { get; set; }
    }
   
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Text=string.Empty;
    }

    public class ThreadExecutor
    {
        public static bool Execute(System.Threading.WaitCallback callback, object state)
        {
            try
            {
                return System.Threading.ThreadPool.QueueUserWorkItem((data) =>
                {
                    try
                    {
                        callback(data);
                    }
                    catch (Exception ex)
                    {
                        //写日志
                    }
                }, state);
            }
            catch (Exception e)
            {
                //写日志
            }
            return false;
        }
        public static bool Execute(System.Threading.WaitCallback callback)
        {
            try
            {
                return System.Threading.ThreadPool.QueueUserWorkItem((data) =>
                {
                    try
                    {
                        callback(data);
                    }
                    catch (Exception ex)
                    {
                        //写日志
                    }
                });
            }
            catch (Exception e)
            {
                //写日志
            }
            return false;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值