|
假如我们有500个帐号需要登录 这时候 如果一次登录肯定会被拦截 我们一次登录5个帐号 c#实现
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace 多线程_队列应用
- {
- class Program
- {
- static Queue<string> m_que = new Queue<string>();
- static Semaphore m_sem=new Semaphore(5,5);
- static void Main(string[] args)
- {
- int length = 500;
- string sname = "";
- for (int i = 0; i < length; i++)//先添加50个帐号数据
- {
- sname = "user_" + i.ToString();
- m_que.Enqueue(sname);
- }
- Thread th_wo = new Thread(work);
- th_wo.Start();
- Console.ReadKey();
- }
- static void work()
- {
- while(m_que.Count>0)
- {
- m_sem.WaitOne();
- var th = new Thread(new ThreadStart(()=>test_que(m_que.Dequeue())));
- th.Start();
- }
- <P> </P>
- }
- static void test_que(string p_name)
- {
- Console.WriteLine(p_name);
- Thread.Sleep(5000);
- m_sem.Release();
- }
- }
- }
复制代码
|
评分
-
查看全部评分
|