C# 带定时器c的静态FIFO队列#
我有一个.NET4.0ASP.NET项目,它需要一些线程工作,我以前从来没有真正搞砸过,我已经看了好几天了,但我还是不知道=/ 基本上,我想要的是,当你在熟食店买了一张票,然后等着轮到你的时候,他们才会给你回复。我会试着把这些联系起来,看看是否有意义 函数启动-->进入需要“获取票据”的部分(我假设在blockingcollection中对某种类型的项目进行排队),并等待其他“票据”(即同一函数的其他实例)完成,然后给函数确定继续(blocking collection进入队列中的项目)-->finish函数 我不需要/不想在队列中做任何工作,我只希望函数在其他函数实例中静态等待轮到它。这有意义吗?可能吗C# 带定时器c的静态FIFO队列#,c#,multithreading,C#,Multithreading,我有一个.NET4.0ASP.NET项目,它需要一些线程工作,我以前从来没有真正搞砸过,我已经看了好几天了,但我还是不知道=/ 基本上,我想要的是,当你在熟食店买了一张票,然后等着轮到你的时候,他们才会给你回复。我会试着把这些联系起来,看看是否有意义 函数启动-->进入需要“获取票据”的部分(我假设在blockingcollection中对某种类型的项目进行排队),并等待其他“票据”(即同一函数的其他实例)完成,然后给函数确定继续(blocking collection进入队列中的项目)-->f
如果可能,请提供代码,因为我已经看到了大量的示例,但没有一个是有意义的/不做我想做的。如果您想使用计时器解决方案,我会将所有操作排队到BlockingCollection中,并让一个专用线程将其出列。此线程将等待5秒,然后将出列的项目推送到线程池中。这个专用线程应该在无限循环中执行此操作。排队,等等,推
class Program
{
static void Main(string[] args)
{
TaskProcessor tp = new TaskProcessor();
Thread t1 = new Thread(new ParameterizedThreadStart(tp.SubmitRequest));
t1.Start(1);
Thread t2 = new Thread(new ParameterizedThreadStart(tp.SubmitRequest));
t2.Start(2);
Thread t3 = new Thread(new ParameterizedThreadStart(tp.SubmitRequest));
t3.Start(3);
}
}
class TaskProcessor
{
private AutoResetEvent _Ticket;
public TaskProcessor()
{
_Continue = new AutoResetEvent(false);
}
public void SubmitRequest(object i)
{
TicketingQueue dt = new TicketingQueue();
Console.WriteLine("Grab ticket for customer {0}", (int)i);
dt.GrabTicket(_Ticket);
_Continue.WaitOne();
Console.WriteLine("Customer {0}'s turn", (int)i);
}
}
public class TicketingQueue
{
private static BlockingCollection<AutoResetEvent> tickets = new BlockingCollection<AutoResetEvent>();
static TicketingQueue()
{
var thread = new Thread(
() =>
{
while (true)
{
AutoResetEvent e = tickets.Take();
e.Set();
Thread.Sleep(1000);
}
});
thread.Start();
}
public void GrabTicket(AutoResetEvent e)
{
tickets.Add(e);
}
}
然而,我实际上建议您使用SemaphoreSlim类来限制对这个脆弱web服务的并发请求的数量。可能您应该选择一个介于1和5之间的数字作为允许的并发量。如果您想要计时器解决方案,我会将所有操作排队到BlockingCollection中,并让一个专用线程将它们出列。此线程将等待5秒,然后将出列的项目推送到线程池中。这个专用线程应该在无限循环中执行此操作。排队,等等,推
然而,我实际上建议您使用SemaphoreSlim类来限制对这个脆弱web服务的并发请求的数量。可能您应该选择1到5之间的数字作为允许的并发量。好吧,在研究了一个又一个文档并反复重写代码之后,我终于发现我没有使用AutoResetEvent权限,以及如何在专用线程上使用阻塞集合。这是使用AutoResetEvent和BlockingCollection的最终解决方案。下面的解决方案可能不会在100%的时间内显示相同的结果(因为我认为这与将某些内容输入到阻塞集合时的线程计时有关),但最终结果是它完全符合我的要求
class Program
{
static void Main(string[] args)
{
TaskProcessor tp = new TaskProcessor();
Thread t1 = new Thread(new ParameterizedThreadStart(tp.SubmitRequest));
t1.Start(1);
Thread t2 = new Thread(new ParameterizedThreadStart(tp.SubmitRequest));
t2.Start(2);
Thread t3 = new Thread(new ParameterizedThreadStart(tp.SubmitRequest));
t3.Start(3);
}
}
class TaskProcessor
{
private AutoResetEvent _Ticket;
public TaskProcessor()
{
_Continue = new AutoResetEvent(false);
}
public void SubmitRequest(object i)
{
TicketingQueue dt = new TicketingQueue();
Console.WriteLine("Grab ticket for customer {0}", (int)i);
dt.GrabTicket(_Ticket);
_Continue.WaitOne();
Console.WriteLine("Customer {0}'s turn", (int)i);
}
}
public class TicketingQueue
{
private static BlockingCollection<AutoResetEvent> tickets = new BlockingCollection<AutoResetEvent>();
static TicketingQueue()
{
var thread = new Thread(
() =>
{
while (true)
{
AutoResetEvent e = tickets.Take();
e.Set();
Thread.Sleep(1000);
}
});
thread.Start();
}
public void GrabTicket(AutoResetEvent e)
{
tickets.Add(e);
}
}
类程序
{
静态void Main(字符串[]参数)
{
TaskProcessor tp=新的TaskProcessor();
线程t1=新线程(新的参数化线程启动(tp.SubmitRequest));
t1.启动(1);
线程t2=新线程(新的参数化线程启动(tp.SubmitRequest));
t2.启动(2);
线程t3=新线程(新的参数化线程启动(tp.SubmitRequest));
t3.启动(3);
}
}
类任务处理器
{
私人汽车租赁券;
公共任务处理器()
{
_Continue=新的自动恢复事件(false);
}
公共无效提交请求(对象一)
{
TicketingQueue dt=新的TicketingQueue();
Console.WriteLine(“为客户{0}抓取票据”,(int)i);
dt.GrabTicket(_Ticket);
_Continue.WaitOne();
Console.WriteLine(“客户{0}轮到我了”,(int)i);
}
}
公共类售票队列
{
私有静态BlockingCollection票证=新建BlockingCollection();
静态票务队列()
{
var线程=新线程(
() =>
{
while(true)
{
AutoResetEvent e=票证。Take();
e、 Set();
睡眠(1000);
}
});
thread.Start();
}
公共罚单(自动恢复事件e)
{
加入(e);
}
}
好吧,在研究了一个又一个文档并反复重写代码之后,我终于发现我没有使用AutoResetEvent权限,也没有在专用线程上使用阻塞集合。这是使用AutoResetEvent和BlockingCollection的最终解决方案。下面的解决方案可能不会在100%的时间内显示相同的结果(因为我认为这与将某些内容输入到阻塞集合时的线程计时有关),但最终结果是它完全符合我的要求
class Program
{
static void Main(string[] args)
{
TaskProcessor tp = new TaskProcessor();
Thread t1 = new Thread(new ParameterizedThreadStart(tp.SubmitRequest));
t1.Start(1);
Thread t2 = new Thread(new ParameterizedThreadStart(tp.SubmitRequest));
t2.Start(2);
Thread t3 = new Thread(new ParameterizedThreadStart(tp.SubmitRequest));
t3.Start(3);
}
}
class TaskProcessor
{
private AutoResetEvent _Ticket;
public TaskProcessor()
{
_Continue = new AutoResetEvent(false);
}
public void SubmitRequest(object i)
{
TicketingQueue dt = new TicketingQueue();
Console.WriteLine("Grab ticket for customer {0}", (int)i);
dt.GrabTicket(_Ticket);
_Continue.WaitOne();
Console.WriteLine("Customer {0}'s turn", (int)i);
}
}
public class TicketingQueue
{
private static BlockingCollection<AutoResetEvent> tickets = new BlockingCollection<AutoResetEvent>();
static TicketingQueue()
{
var thread = new Thread(
() =>
{
while (true)
{
AutoResetEvent e = tickets.Take();
e.Set();
Thread.Sleep(1000);
}
});
thread.Start();
}
public void GrabTicket(AutoResetEvent e)
{
tickets.Add(e);
}
}
类程序
{
静态void Main(字符串[]参数)
{
TaskProcessor tp=新的TaskProcessor();
线程t1=新线程(新的参数化线程启动(tp.SubmitRequest));
t1.启动(1);
线程t2=新线程(新的参数化线程启动(tp.SubmitRequest));
t2.启动(2);
线程t3=新线程(新的参数化线程启动(tp.SubmitRequest));
t3.启动(3);
}
}
类任务处理器
{
私人汽车租赁券;
公共任务处理器()
{
_Continue=新的自动恢复事件(false);
}
公共无效提交请求(对象一)
{
TicketingQueue dt=新的TicketingQueue();
Console.WriteLine(“为客户{0}抓取票据”,(int)i);
dt.GrabTicket(_Ticket);
_Continue.WaitOne();
Console.WriteLine(“客户{0}轮到我了”,(int)i);
}
}
公共类售票队列
{
私有静态BlockingCollection票证=新建BlockingCollection();
静态票务队列()
{
var线程=新线程(
() =>
{
while(true)
{
AutoResetEvent e=票证。Take();
e、 Set();
睡眠(1000);
}
});
thread.Start();
}
公共罚单(自动恢复事件e)