引入命名空间 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);