/// <summary>
/// 多线程调用WCF
/// </summary>
/// <param name="select">调用WCF的方式,1=Restful,2=Tcp</param>
/// <param name="num"></param>
static void DoTest_MultiThread(string select, long num)
{
int n_max_thread = 10; // 设置并行最大为10个线程
int n_total_thread = 0; // 用来控制:主程序的结束执行,当所有任务线程执行完毕
ILog log_add = new LogHelper("Add_Thread");
ILog log_del = new LogHelper("Del_Thread");
ILog log_wait = new LogHelper("Wait_Thread");
ILog log_set = new LogHelper("Set_Thread");
ILog log_for = new LogHelper("For_Thread");
Console.Title = string.Format("调用WCF的方式 => {0}, 调用次数=> {1}"
, select == "1" ? "Restful" : "Socket"
, num);
List<int> list_Thread = new List<int>();
System.Threading.AutoResetEvent wait_sync = new System.Threading.AutoResetEvent(false); // 用来控制:并发最大个数线程=n_max_thread
System.Threading.AutoResetEvent wait_main = new System.Threading.AutoResetEvent(false); // 用来控制:主程序的结束执行,当所有任务线程执行完毕
DateTime date_step = DateTime.Now;
for (long i = 0; i < num; i++)
{
Num_Query_Static++;
if (i >0 && (i+1-1) % n_max_thread == 0) // -1 表示第max个线程尚未开始
{
//log_wait.Info(string.Format("thread n= {0},for i= {1}", dic_Thread.Count, i + 1));
wait_sync.WaitOne(); // 每次并发10个线程,等待处理完毕后,在发送下一次并发线程
}
log_for.Info(string.Format("thread n= {0},for i= {1}", list_Thread.Count, i + 1));
System.Threading.ThreadPool.QueueUserWorkItem
((data) =>
{
int id = System.Threading.Thread.CurrentThread.ManagedThreadId;
System.Threading.Monitor.Enter(list_Thread);
list_Thread.Add(id);
System.Threading.Monitor.Exit(list_Thread);
log_add.Info(string.Format("id={0}, count={1}", id, list_Thread.Count)); // 日志
if (select == "1") // Restful方式调用
{
Query_Htty();
}
else
{
Query_Socket();
}
n_total_thread += 1;
if (list_Thread.Count == (n_max_thread) || n_total_thread == num)
{
list_Thread.Clear();
//log_set.Info(string.Format("thread n= {0},for i= {1}", dic_Thread.Count, i + 1));
//wait_sync.Set();
if (n_total_thread != num)
{
wait_sync.Set(); // 任务线程,继续执行
}
else
{
wait_main.Set(); // 主程序线程,继续执行
}
}
}, list_Thread);
}
wait_main.WaitOne();
Console.WriteLine(string.Format("总测试{0}次,总耗时{1}, 平均耗时{2}"
, num
, (DateTime.Now - date_step).ToString()
, (DateTime.Now - date_step).TotalMilliseconds / num));
Query_Thread();
}
private static ILog log = new LogHelper("SeqService"); // 日志
private static Dictionary<int, DateTime> dic_thread = new Dictionary<int, DateTime>(); // 线程列表
private static long Num = 0; // 线程个数
private static object lock_Num = 0; // 共享数据-锁
/// <summary>
/// 在线申请流水号
/// </summary>
/// <returns></returns>
[WebGet(UriTemplate = "GetSeqNum/Json", ResponseFormat = WebMessageFormat.Json)]
public string GetSeqNumber()
{
lock (lock_Num)
{
Num++;
int id_thread = System.Threading.Thread.CurrentThread.ManagedThreadId;
DateTime now = DateTime.Now;
if (!dic_thread.TryGetValue(id_thread, out now))
{
dic_thread.Add(id_thread, DateTime.Now);
}
}
string ret = DateTime.Now.ToString("yyyyMMdd") + Num.ToString(new string('0', 9));
log.Info(string.Format("{0}, Thread={1}/{2}", ret, System.Threading.Thread.CurrentThread.ManagedThreadId, dic_thread.Count));
return ret;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有