110,534
社区成员
发帖
与我相关
我的任务
分享
static int index = 0;
static string[] persons = {"person1","person2","person3"};
static void Main(string[] args)
{
for (int threadIndex = 0; threadIndex < 10; threadIndex++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(GetPos));
}
}
static void GetPos()
{
//获取person
string usedPerson = persons[index];
//doing sth
...
index++;
}
今天随便写了一段小程序,虽然不能用信号量存储信息,但可以借助Semaphor和lock()方法来完成,没有经过严格测试,如果有兴趣的话,希望大家帮忙测试一下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SemaphoreDemo
{
public class ThreadClass//声明一个类
{
public IList<PersonWorker> PersonList { get; set; }//传入共享资源
private Semaphore se = null;//信号量
public ThreadClass(Semaphore Semaphore)//创建类实例
{
se = Semaphore;
}
public void Work()//让类开始工作
{
try
{
Console.WriteLine(Name + " Wait Semaphore " + DateTime.Now.ToString("HH:mm:ss"));
se.WaitOne();//等待信号量
Console.WriteLine(Name + " Got Semaphore " + DateTime.Now.ToString("HH:mm:ss"));
PersonWorker Worker = null;//准备获取工作者
lock (PersonList)//锁定共享资源,若到这一步,则说明一定存在共享资源,否则会由信号量一直等待直到有线程释放了共享资源
{
foreach (PersonWorker pw in PersonList)
{
if (!pw.IsWorking)//
{
Worker = pw;//取得空闲的共享资源
break;
}
}
}
Worker.Run();//开始工作;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine(Name + " Release Semaphore " + DateTime.Now.ToString("HH:mm:ss"));
Console.WriteLine("*************************");
Console.WriteLine("");
se.Release();//释放信号量
}
}
public string Name { get; set; }
}
public class PersonWorker
{
private bool m_IsWorking = false;
public bool IsWorking { get { return m_IsWorking; } }
public string Name { get; set; }
static Random r = null;
static PersonWorker()
{
r = new Random();
}
public void Run()
{
int l = r.Next(10, 10000);//随机生成工作的时间,
Console.WriteLine("Thread=" + Name + " Time=" + l.ToString());
m_IsWorking = true;
Thread.Sleep(l);//模拟工作的时间,
m_IsWorking = false;//工作完成,
}
}
}
下面是主程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace SemaphoreDemo
{
class Program
{
private static Semaphore se;//信号量
private static List<PersonWorker> PersonList;//共享资源
private static int i = 0;//创建的线程个数
private static TextWriter tw = null;//把结果写入到当前目录的文本文件,以便于分析程序是否正确
static void Main(string[] args)
{
PersonList = new List<PersonWorker>() {
new PersonWorker(){Name="P0"},
new PersonWorker(){Name="P1"},
new PersonWorker(){Name="P2"}
};//创建共享资源
se = new Semaphore(PersonList.Count, PersonList.Count);//创建信号量
tw = new StreamWriter("log" + DateTime.Now.ToString("ddHHmmss") + ".txt");
Console.SetOut(tw);//
Timer t = new Timer(ThreadCreator);//创建一定时器
t.Change(1000, 1000);//1秒触发一次
Console.ReadKey();
t.Dispose();
tw.Close();
}
private static void ThreadCreator(object o)
{
Console.WriteLine("");
Console.WriteLine("Create Thread");
ThreadClass tc = new ThreadClass(se) { PersonList = PersonList, Name = "Thr" + i++.ToString() };//创建线程,传入信号量及共享资源
tc.Work();//让线程开始工作;
}
}
}
Queue<Person> personQueue;
void StartProcessing()
{
Person[] persons = ...;
personQueue = new Queue<Person>(persons);
for (int i = 0; i < 8; i++)
{
ThreadPool.QueueUserWorkItem( ProcessPerson );
}
}
void ProcessPerson(object state)
{
while (true)
{
Person person = null;
lock (this.personQueue)
{
if (this.personQueue.Count > 0) person = personQueue.Dequeue();
else return;
}
person.DoSomething();
}
}