使用多线程将数据同时插入到列表中

可可

我正在尝试优化一个小程序。所以这里是基本的想法:

我有一组未过滤的数据,我想将它传递给一个函数,该函数将调用另一个函数两次,用于数据过滤和插入新列表。第一次调用将从原始数组中获取数据,范围为 0 => 数组长度的一半,第二次调用将执行相同的操作,但范围从一半到最后一项。这样,我应该将过滤后的数据同时插入到同一个列表中。插入完成后,过滤后的列表可以传递给程序的其余部分。这是代码:

    static void Main(string[] 
        {
            // the unfiltered list
            int[] oldArray = new int[6] {1,2,3,4,5,6};
            // filtered list
            List<int> newList= new List<int>();
            // Functions is my static class
            Functions.Insert(newList, oldArray )
            Continue_Program_With_Filtered_List(newList);
            // remaining functions...
        }

这是函数类:

public static class Functions
{
    public static void Insert(List<int> newList, int[] oldArray)
    {
        new Thread(() =>
        {
            Inserter(newList, oldArray, true);
        }).Start();
        new Thread(() =>
        {
            Inserter(newList, oldArray, false);
        }).Start();

        // I need to wait the result here of both threads
        // and make sure that every item from oldArray has been filtered
        // before I proceed to the next function in Main()
    }

    public static void Inserter(List<int> newList, int[] oldArray, bool countUp)
    {
        bool filterIsValid = false;
        int length = oldArray.Length;
        int halflen = (int)Math.Floor((decimal)length / 2);
        if (countUp)
        {
            // from half length to 0
            for (int i = 0; i < halflen; i++)
            {
                // filtering conditions here to set value of filterIsValid
                if(filterIsValid)
                    newList.Add(oldArray[i]);
            }
        }
        else
        {
            // from half length to full length
            for (int i = halflen + 1; i < length; i++)
            {
                // filtering conditions here to set value of filterIsValid
                if(filterIsValid)
                    newList.Add(oldArray[i]);
            }
        }
    }
}

所以问题是我必须等待 Function.Insert() 来完成每个线程,并在 newList 传递给 Main() 中的下一个函数之前通过每个项目

我不知道如何在这样的事情上使用任务或异步方法。顺便说一下,这只是程序的概要。有什么帮助吗?

鼻子

在您的情况下,使用PLINQ也可能是一种选择。

static void Main(string[] args)
{
    // the unfiltered list
    int[] oldArray = new int[6] { 1, 2, 3, 4, 5, 6 };
    // filtered list
    List<int> newList = oldArray.AsParallel().Where(filter).ToList();
    // remaining functions...
}

您还可以使用AsOrdered()来保持顺序


回到您最初的问题,这是您可以做的
注意:对原始代码进行最少更改的解决方案,无论是否有其他可能的优化
附加注意:请记住,仍然可能存在并发问题,具体取决于您的其他内容处理传递给该函数的参数。

public static async Task Insert(List<int> newList, int[] oldArray)
{
    ConcurrentBag<int> concurrentBag = new ConcurrentBag<int>();
    var task1 = Task.Factory.StartNew(() =>
    {
        Inserter(concurrentBag, oldArray, true);
    });
    var task2 = Task.Factory.StartNew(() =>
    {
        Inserter(concurrentBag, oldArray, false);
    });
    await Task.WhenAll(task1, task2);
    newList.AddRange(concurrentBag);
}

public static void Inserter(ConcurrentBag<int> newList, int[] oldArray, bool countUp)
{
    //Same code
}

编辑:您的第二个for-loop是错误的,将其更改为这个,否则您将丢失一项

for (int i = halflen; i < length; i++)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在C ++中通过多线程将数据同时插入到单个MySQL表中

使用多线程模块将API数据检索到数据帧中

线程如何使用java将增量数据插入到Android studio中的textveiw中

如何使用 Excel 中的 ListBox.List 将数据从搜索功能插入到列表中?

在多线程程序中缓冲数据库插入

如何在多线程Java中插入数据库

检查数据库中是否存在数据,如果未找到则使用多线程插入新数据?

使用pybind11将Python解释器嵌入到多线程C ++程序中

使用Java中的多线程将大量文件从本地复制到DBFS服务器

在将新数据插入到PostgreSQL表中的同时,是否可以转储/复制PostgreSQL表?

如何将多个行数据同时插入到两个表中

在pika使用者中运行多处理/多线程并将数据定向到特定数据框

使用python将json数据插入到postgres表中

Python-使用多线程处理列表中的元素

无法使用SpringMVC通过Hibernate将ItemDetail的列表(即List <ItemDetail>(OBJECT))插入到MySQL数据库中

使用Mutex防止同时处理相同类型时进行多线程列表迭代

使用Swift使用多线程将数组中的项目组合

将列表中的数据插入到mysql数据库中

如何使用多线程在MySQL中获取数据

多线程同时异步

使用多个线程将数据写入文本文件(同时,在文件的不同行中)

使用gspread将python中的列表列表插入到Google表格中

Python多线程使用另一个线程中的一个线程的数据

根据计数有条件地将记录插入多线程环境中的表中

使用ajax将已加载的数据插入页面源中以将其插入到mysql中

将列表数据插入到iext7 pdf中的表中(xamarin android)

加密++可以使用[私有]密钥对象同时使用[在多线程中]

使用线程生产并消费到列表中?

使用ls将文件存储到列表中,同时使用sed删除部分目录名