在多线程中实现队列 [英] Implement queues in Multithreading

查看:80
本文介绍了在多线程中实现队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Image中解释整个场景。多个客户端使用call()调用函数名称到service1。并且每个客户端同时调用函数。所以service1标识该函数调用来自哪个客户端,并且每个客户端调用一个函数(call())的时间超过10倍的毫秒差异,而我的要求是,以什么顺序客户端调用该函数,以相同的顺序调用函数应该执行,并行这应该适用于所有客户。



图像链接。 请点击此处



当前实施工作:



客户1

客户2



* * *



客户端N



client1在10miliseconds差异时调用10次函数。



client1.call(1);



client1.call(2);



client1 .call(3);



client1.call(4);



client1.call(5 );



client1.call(6);



client1.call(7);



client1.call(8);



client1.call(9);



client1.call(10);



所有客户同时致电服务1



Service1 Call();



然后service1调用call()函数,其实现在service2中。



服务2(Wcf服务)



public int Call(int number)

{

Console.WriteLine(参数传递中的数字:+数字);

返回数字;



}

输出来了



参数传递中的数字:1



参数传递中的数字:2



参数传递中的数字:5



参数传递中的数字:6



参数传递中的数字:7



参数传递中的数字:8



参数传递中的数字:3



参数传递中的数字:4



参数传递中的数字:10



参数传递中的数字:9

输出顺序打扰,按要求输出



参数传递中的数字:1



参数传递中的数字:2



参数传递中的数字:3



参数传递中的数字:4



参数传递中的数字:5



参数传递中的数字:6



参数传递中的数字:7



参数传递中的数字:8



参数传递中的数字:9



参数传递中的数字:10

这是因为call()函数同时调用10次而call(3)和call(4)需要时间在数据库中插入数据并调用(5),call(6)首先完成。





果壳:



如何为每个客户排队,以便订购不会打扰每个客户。



我试过这个ThreadPool.QueueUserWorkItem(state =>调用(1)),但QueueUserWorkItem将只返回布尔值,但我需要返回特定的自定义列表。这是通过call()函数返回。

In Image whole scenario is explained. that the multiple number of clients calls a function name with call(), to service1. and each client call function simultaneously. so service1 identity that function call from which client, and each client calls a function (call()) at a time more than 10times on miliseconds difference, and my requirement is, at what order client call the function, in the same order call function should execute, and parallel this should work for all clients.

Image link. click here

current Implemetation working:

client 1
Client2

* * *

client N

client1 calls function 10 times at 10miliseconds difference.

client1.call(1);

client1.call(2);

client1.call(3);

client1.call(4);

client1.call(5);

client1.call(6);

client1.call(7);

client1.call(8);

client1.call(9);

client1.call(10);

All clients simultaneously call function to service 1

Service1 Call();

And then service1 calls the call() function whose implementation is in service2.

Service 2 (Wcf service)

public int Call(int number)
{
Console.WriteLine("The number in parameter pass :" + Number);
return number;

}
Output Comes

The number in parameter pass :1

The number in parameter pass :2

The number in parameter pass :5

The number in parameter pass :6

The number in parameter pass :7

The number in parameter pass :8

The number in parameter pass :3

The number in parameter pass :4

The number in parameter pass :10

The number in parameter pass :9
In output order disturb, as required output is

The number in parameter pass :1

The number in parameter pass :2

The number in parameter pass :3

The number in parameter pass :4

The number in parameter pass :5

The number in parameter pass :6

The number in parameter pass :7

The number in parameter pass :8

The number in parameter pass :9

The number in parameter pass :10
This is happening due to call() function simultaneously calls 10times and call(3) and call(4) takes time to insert data in database and call(5), call(6) complete first.


In Nutshell:

How do i make a queues for each client, so that order does not disturb for each client.

I tried like this ThreadPool.QueueUserWorkItem(state => Call(1)), but QueueUserWorkItem will return only Boolean vlaue, but i need to get in return specific custom list. which is return by call() function.

推荐答案

hreadPool.QueueUserWorkItem(state => Call(1))将按顺序排列项目。这确保了将按照该顺序开始但不保证它们将按照该顺序完成。



如何使用标准程序调用来拨打电话1- n并将每个客户端的程序抛入线程池。



请记住,Threadpool只需要知道运行什么方法

The hreadPool.QueueUserWorkItem(state => Call(1)) will queue the items in order. This does guarantee that the will start in that order but it does not guarantee that they will complete in that order.

How about using a standard procedural call for call 1-n and throw that procedure for each client into the threadpool.

Remember that Threadpool only needs to know what method to run
public void RunCallsInSequence(Client client){

//Generate calls
//call(1)
//call(2)
//...
//call(n)

}

public void RunClientsInThreads(Client[] clients){

 foreach(Client client in Clients)
 {
  ThreadPool.QueueUserWorkItem(state => RunCallsInSequence(client))
 }
}





或者您甚至可以使用lamda代表按顺序管理您的电话:



Or you can even use the lamda delegate to manage your calls in order:

public void RunClientsInThreads(Client[] clients){

 foreach(Client client in Clients)
 {
  ThreadPool.QueueUserWorkItem(state => {
   //Generate calls
   //call(1)
   //call(2)
   //...
   //call(n)
  })
 }
}





我不知道你是如何产生电话所以我不能更具体



如果这种方法不符合您的需求,请告诉我,为什么,我会优化我的解决方案



谢谢

Andy ^ _ ^



I don't know how you generate calls so I can't be more specific

Please let me know if this approach doesn't suit your needs, and why, and I'll refine my solution

Thanks
Andy ^_^


这篇关于在多线程中实现队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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