C# 线程池(ThreadPool)批量保存数据

执行完没别的操作

效果图

在这里插入图片描述

实际代码

实体类

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

线程池传递的参数

public class ThreadPoolParamModel
{
    public ThreadPoolParamModel()
    {
        list = new List<Person>();
    }
    public List<Person> list { get; set; }
}

保存数据逻辑

/// <summary>
/// 保存数据
/// </summary>
/// <param name="param"></param>
private static void Save(object param)
{
    var model = (ThreadPoolParamModel)param;
    //写下保存的逻辑
    Console.WriteLine("保存数据");
    Thread.Sleep(1000);
    Interlocked.Increment(ref pageIndex);
    if (pageIndex == pageCount)
    {
        Console.WriteLine("所有线程池执行结束");
        _event.Set();
    }
}

主要代码(可以根据实际需求进行改动)

public static int pageIndex = 0;//某个线程池
public static int pageCount = 0;//最大线程池
public static ManualResetEvent _event = new ManualResetEvent(false);
public static void Main(string[] args)
{
    List<Person> list = new List<Person>();
    list.Add(new Person { Id = 1, Name = "张三", Age = 10 });
    list.Add(new Person { Id = 2, Name = "李四", Age = 20 });
    list.Add(new Person { Id = 3, Name = "王五", Age = 30 });
    int count = list.Count();//要保存的所有记录
    int pageSize = 2;//每次保存的记录
    pageCount = (count % pageSize) == 0 ? (pageSize / pageSize) : (pageSize / pageSize + 1);//计算可开的线程池数
    for (var i = 0; i < pageCount; i++)
    {
        ThreadPoolParamModel param = new ThreadPoolParamModel();
        param.list = list.Skip(pageSize * (i - 1)).Take(pageSize).ToList();//取出pageSize笔记录
        ThreadPool.QueueUserWorkItem(new WaitCallback(Save), param);
    }
    Console.WriteLine("等待线程池完成操作......");
    _event.WaitOne(Timeout.Infinite, true);
    Console.WriteLine("程序执行完成");
    Console.ReadLine();
}

执行完有别的逻辑

效果图

在这里插入图片描述

实际代码

实体类

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

线程池传递的参数

public class ThreadPoolParamModel
{
    public ThreadPoolParamModel()
    {
        list = new List<Person>();
    }
    public List<Person> list { get; set; }
}

保存数据逻辑

/// <summary>
/// 保存数据
/// </summary>
/// <param name="param"></param>
private static void Save(object param)
{
    var model = (ThreadPoolParamModel)param;
    //写下保存的逻辑
    Console.WriteLine("保存数据");
}

所有线程执行完成后执行的逻辑

/// <summary>
/// 所有线程执行完成后执行的逻辑
/// </summary>
/// <param name="str"></param>
private static void WriteStr(string str)
{
    Console.WriteLine(str);
}

主要代码

public static void Main(string[] args)
{
    RegisteredWaitHandle rhw = null;

    List<Person> list = new List<Person>();
    list.Add(new Person { Id = 1, Name = "张三", Age = 10 });
    list.Add(new Person { Id = 2, Name = "李四", Age = 20 });
    list.Add(new Person { Id = 3, Name = "王五", Age = 30 });
    int count = list.Count();//要保存的所有记录
    new Action(() =>
    {
        int pageSize = 2;//每次保存的记录
        var pageCount = (count % pageSize) == 0 ? (pageSize / pageSize) : (pageSize / pageSize + 1);//计算可开的线程池数
        for (var i = 0; i < pageCount; i++)
        {
            new Action<int>((index) =>
            {
                ThreadPoolParamModel param = new ThreadPoolParamModel();
                param.list = list.Skip(pageSize * (index - 1)).Take(pageSize).ToList();//取出pageSize笔记录
                ThreadPool.QueueUserWorkItem(new WaitCallback(Save), param);
            }).BeginInvoke(i, null, null);
        }
    }).BeginInvoke(null, null);
    rhw = ThreadPool.RegisterWaitForSingleObject(new AutoResetEvent(false), new WaitOrTimerCallback((obj, b) =>
    {
        int workerThreads = 0;
        int maxWordThreads = 0;
        int compleThreads = 0;
        ThreadPool.GetAvailableThreads(out workerThreads, out compleThreads);
        ThreadPool.GetMaxThreads(out maxWordThreads, out compleThreads);
        //当可用的线数与池程池最大的线程相等时表示线程池中所有的线程已经完成
        if (workerThreads == maxWordThreads)
        {
            rhw.Unregister(null);
            WriteStr("所有线程执行完成后执行的逻辑");
            rhw = null;
        }
    }), null, 100, false);
    Console.WriteLine("程序执行完成");
    Console.ReadLine();
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值