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;
}
}
}