C#多线程的使用
来源:互联网 发布:mysql engine=myisam 编辑:程序博客网 时间:2024/06/04 08:41
引入命名空间 using System.Threading;
方式1:手动创建线程
//开启多线程执行任务 Thread thread1 = new Thread(a => { InsertEs(0, 200000);//执行写入数据区间 }); thread1.Start(); Thread thread2 = new Thread(a => { InsertEs(200000, 400000); }); thread2.Start(); Thread thread3 = new Thread(a => { InsertEs(400000, 600000); }); thread3.Start(); Thread thread4 = new Thread(a => { InsertEs(600000, int.MaxValue); }); thread4.Start(); Thread thread5 = new Thread(a => { //调用创建手动录入的用户数据写入索引 CreateAllDocIndexForManual(client); }); thread5.Start(); bool status = true; while (status)//状态为True说明还有线程没有跑完 { if (thread1.ThreadState == ThreadState.Running || thread2.ThreadState == ThreadState.Running || thread3.ThreadState == ThreadState.Running || thread4.ThreadState == ThreadState.Running || thread5.ThreadState == ThreadState.Running) { status = true; } else { status = false; } }
方式二:线程池
//创建4个线程同时进行 var taskArr = new List<Task>(); taskArr.Add(Task.Run(() => { InsertEs(0, 200000); })); taskArr.Add(Task.Run(() => { InsertEs(200000, 400000); })); taskArr.Add(Task.Run(() => { InsertEs(400000, 600000); })); taskArr.Add(Task.Run(() => { InsertEs(600000, int.MaxValue); })); taskArr.Add(Task.Run(() => { //调用创建手动录入的用户数据写入索引 CreateAllDocIndexForManual(client); })); Task.WaitAll(taskArr.ToArray(),10000);
阅读全文
0 0
- C#多线程的使用
- C#多线程的使用
- C# 多线程的使用(Thread)-初识多线程
- c#4.0多线程的使用
- Unity使用C#的多线程
- c#多线程 task的使用
- C#多线程的使用(Thread)-多线程的挂起,恢复操作
- c#多线程 Invoke方法的使用
- c#多线程 Invoke方法的使用
- C#多线程 Invoke方法的使用收藏
- C#多线程 Invoke方法的使用收藏
- c#多线程 Invoke方法的使用
- c#多线程 Invoke方法的使用
- c#多线程中Lock()关键字的使用
- C#中多线程 委托的使用
- c#多线程 Invoke方法的使用
- C#多线程中锁的使用
- C#多线程Thread.Abort的使用
- Spring面试题(70道,史上最全)
- 数据的提交与接收-get与post区别
- maven 部署在tomcat
- io流实现读取文件
- 欢迎使用CSDN-markdown编辑器
- C#多线程的使用
- Linux
- 在 Azure 上的 Ubuntu Docker 运行 SQL Server
- 转:大话Ceph--CRUSH那点事儿
- h5 web页面手机适配
- AMD Ryzen CPU 在VMware中安装Mac OS 10.11
- perl 中的面向对象多继承
- 2017派卧底去阿里、京东、美团、滴滴带回来的面试题及答案
- 使用聚合函数查询