从多个线程按顺序获取结果 [英] Getting results in order from multiple threads

查看:68
本文介绍了从多个线程按顺序获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是将MySQL数据库查询制成部分并执行不同线程中的每个部分,例如



如果用户想要查看1中的记录到1000然后查询将是(从记录中选择*,其中rec_no> = 1并且rec_no< = 1000)。现在我想运行4个查询而不是运行这个查询,例如



1st query =(从记录中选择*,其中rec_no> = 1和rec_no< = 250 )

第二次查询=(从记录中选择*,其中rec_no> = 251和rec_no< = 500)

第三次查询=(从记录中选择*,其中rec_no> = 501和rec_no< = 750)

第4次查询=从记录中选择*,其中rec_no> = 751和rec_no< = 1000)



这样所有查询都会花费一小部分时间并且并行和快速执行。

I我从所有查询得到结果,但只面临问题,在单个数组中积累它们,这将保持结果和原因,任何线程将首先执行并返回结果。



部分代码是



  private   void  btnStartThread_Click( object  sender,System.EventArgs e)
{
Thread [] m_WorkerThread = new Thread [Environment.ProcessorCount];


// 创建工作线程实例
for int i = 0 ; i < Environment.ProcessorCount; i ++)
{
m_WorkerThread [i] = new Thread(WorkerThreadFunction));

m_WorkerThread [i] .Name = 工人线程样本 + i ;

m_WorkerThread [i] .Start( querylist [i]) ;
}
}





  private   void  WorkerThreadFunction( string  s)
{
< span class =code-comment> // Thread [] t = new Thread [Environment.ProcessorCount];

MyDefinedClass IClass;

lClass = new MyDefineClass();

Iclass.Run(s);

}





最后我得到的结果

  private   void  AddString(array [] s)
{

final_array // 将逐个添加记录,即查询1的结果,然后查询2
// 按顺序查询第4行。但我不知道返回我的线程//结果因为任何线程都可以先返回结果
}





提前谢谢

解决方案

我看到了很多不正确的假设......



首先,除非每个线程都创建自己与数据库的连接(这很耗时),否则您的查询(尽管是在不同的线程中)会一次发送和执行一个。 MySQL连接一次只能处理一个查询。



其次,线程将以任何顺序随时执行。您无法让他们按预定顺序返回结果。你要做的就是将所有记录添加到你的集合中,然后对最终结果进行排序(这非常耗时)。



现在,是你的MySQL服务器与你的代码在同一台机器上运行??将4个线程放在一起根本不会做任何事情,因为MySQL服务器将不仅与你的线程竞争CPU时间,而且还在运行系统上的数百个其他线程。



我不知道当你需要花费更多的时间来组装和清理你的查询时,你需要通过线程来提高性能。

Hi, my requirement is to brake the MySQL database query into parts and execute each part within different threads for example

if user want to see the record from 1 to 1000 then query would be (Select * from record where rec_no > = 1 and rec_no <= 1000). Now instead of running this query I want to run 4 queries such as

1st query= (Select * from Record where rec_no > = 1 and rec_no <= 250)
2nd query = (Select * from Record where rec_no > = 251 and rec_no <= 500)
3rd query =(Select * from Record where rec_no > = 501 and rec_no <= 750)
4th query =Select * from Record where rec_no > = 751 and rec_no <= 1000)

this way all queries would take fraction of second and get executed parallel and fast.
I am getting result from all query but only facing problem to accumulate them in single array which would hold the result and reason behind that any thread would get executed first and return me the result.

part of my code is

private void btnStartThread_Click(object sender, System.EventArgs e)
		{
            Thread[] m_WorkerThread = new Thread[Environment.ProcessorCount];
			

	  // create worker thread instance
            for (int i = 0; i < Environment.ProcessorCount; i++)
            {
                m_WorkerThread[i] = new Thread(WorkerThreadFunction));

                m_WorkerThread[i].Name = "Worker Thread Sample" + i;	

                m_WorkerThread[i].Start("querylist[i]");
            }
		}



private void WorkerThreadFunction( string s)
		{
            //Thread[] t = new Thread[Environment.ProcessorCount];
            
                MyDefinedClass IClass;

                lClass = new MyDefineClass();

                Iclass.Run(s);
            
		}



finally I am getting result in

private void AddString(array[] s)
		{
			
       final_array // would add record one by one, that is result of query 1, then query 2 
                   // till query 4 in order. but I am not aware of thread that is returning me           //result as any thread can return result first                  	
		}



Thanks in advance

解决方案

I see a ton of incorrect assumptions with this...

First, unless each of those threads is creating it's own connection to the database (which is time consuming), your queries, though in separate threads, are being sent and executed one at a time. A MySQL connection will only work on one query at a time.

Second, threads will execute in any order and at any time. You cannot get them to return results in a predetermined order. What you do to compensate for this is to add all the records to your collection, then sort the end result (which is time consuming).

Now, is your MySQL server running on the same machine as your code?? Putting together 4 threads isn't going to do anything at all since the MySQL server is going to be competing for CPU time with, not only your threads, but the hundreds of other threads on the system that are running.

I don't see how you're going to get a performance boost by threading when you have to pay for that boost with more time to assemble and clean up your query.


这篇关于从多个线程按顺序获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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