逐个出队元素,然后将此值存储到MYSQL数据库 [英] dequeue the element one by one then stored this value to the MYSQL database

查看:76
本文介绍了逐个出队元素,然后将此值存储到MYSQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想逐个出集合的元素,然后将它们的值存储在MYSQL数据库中.
当第一个元素出队时,我想启动一个线程来更新数据库,然后,当第二个元素出队时,我想启动另一个线程.
当第一个线程完成执行时,我想重用它来处理队列中的第三个元素.然后,第二个线程完成执行后,我想重用它来处理队列中的第四个元素.我想继续这种方式,直到队列中的所有元素都处理完.
这些所有进程都在1ms处处理另一个线程.
有谁知道如何使用两个线程一个接一个地处理集合吗?将元素一个接一个出队,然后将此值存储到MYSQL数据库中.
当第一个元素出队时,然后启动另一个线程来更新数据库,
然后第二个元素出队,然后启动另一个线程,当第一个完成执行时,处理队列的第三个元素,然后第二个处理第四个元素&等等..
这些所有进程都在1ms处处理另一个线程.
如果有人知道如何一个线程一个线程地启动,请告诉我解决方案.

I want to dequeue the elements of a collection one by one then store their values in a MYSQL database.
When the first element is dequeued I want to start a thread to update the database, then when the second element is dequeued I want to start another thread.
When the first thread finishes execution, I want to reuse it handle the third element of in the queue. Then, after the second thread finishes execution, I want to reuse it to handle the fourth element in the queue. I want to continue this way until all the elements in the queue are processed.
These all process handle the another thread at 1ms.
Does anyone know how to process a collection one by one using two threads?dequeue the element one by one then stored this value to the MYSQL database.
when first element dequeue then start another thread to update the database,
then second element dequeue then start another thread , when first finish execution handle the third element of the queue then second handle the fourth element & so on..
these all process handle the another thread at 1ms.
if any one known to how to start the one by one thread then tell me the solution.
thanks in advance.

推荐答案

使队列出队< T>或ConcurrentQueue< T>并使用 ThreadPool.QueueUserWorkItem [
Dequeue your Queue<T> or ConcurrentQueue<T> and use ThreadPool.QueueUserWorkItem[^] to create threads that handles MySql INSERT statements.

But this can make a high load on MySql and connection creations. Perhaps it is better to do chunks of inserts ?


这篇关于逐个出队元素,然后将此值存储到MYSQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆