需要伪代码/维护X个线程的代码 [英] need pseudocode/code of maintaining X number of threads

查看:57
本文介绍了需要伪代码/维护X个线程的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VB.NET 2005,以及在双CPU盒上运行的Windows窗体,我需要

获取记录集(例如100,000条记录)并生成一个线程来处理

每条记录的互联网XML事务例程。这是一个很好的

使用线程,因为那些互联网请求是针对第三方

服务器,这些服务器通常有1秒的延迟问题,因此处理它们

多线程是获取<​​br />
记录集中所有记录的最快方法。我想设置一个变量/常量来控制允许同时运行并允许代码保持的线程的数量



产生的线程数达到数字。然后,我可以使用那个数字和

找出最好的#个线程。但我不是线程专家

而且我不知道我对.NET 2.0框架有什么新的选择。我也没有找到任何一个例子,显示线程被重复产生到一个特定数字,以努力通过一个记录集。显然

每个线程完成后需要与主例程进行通信(触发

事件??)并且主例程需要接下来的记录并且<如果#threads小于max#threads,则
将其抛给另一个线程。可以

有人请帮我用这个伪代码,或者更好的是,给我一个真正的代码示例指向我这个吗?

解决方案

HK,


您获得带记录集的线程示例的更改几乎相等

to 0,001%


也许如果你第一次尝试从中制作一个数据表,你的更改将会高出



您还必须知道您使用的协议。标准将HTTP 1.1

不能同时处理超过2个连接(这是可调整的)。


因此除了线程之外你还有很多其他的要做的问题。首先制作你的

程序而不用线程可能是一个更好的方法。添加

比一些线程主要不是什么大挑战。


只是我的想法,


Cor


我不明白你的想法,我不会得到太多的好处。我看到它有点不同,但也许我错了。也许我的规格还不清楚。


除了HTTP连接限制外,我会从这里开始:

---------- -------------------



发送XML请求

获取XML响应互联网延迟后1秒钟

直到所有100,000个记录都被用完

----------------------- ------


对此:


----------------- ------------

如果线程可用,请



spawn这个记录的任务

结束如果

直到所有100,000个记录都已用完


子任务

发送XML请求

在网络延迟后1秒钟获得XML响应

告知线程已完成回主循环

End Sub

----- ------------------------

每项任务花费的大部分时间都在等待互联网延迟。如果我有
有10个工蜂(线程)都记录在记录集中的下一条记录,那么
每秒可能有10个响应而不是每个响应1个>
秒,我可以快10倍地记录我的100,000条记录。带宽是

不是瓶颈。看起来像一个理想的线程应用程序,而另一端的公司已经同意了。但是当然可以告诉我,如果我错了。


我只需要更好的伪代码,我上面写的因为我不知道

如何跟踪可用的线程以及如何在下一个记录中产生下一个线程




" Cor Ligthert [MVP]" <无************ @ planet.nl>在留言中写道

新闻:O9 ************** @ TK2MSFTNGP09.phx.gbl ...

HK,
你得到一个带有记录集的线程样本的变化几乎是
等于0,001%

如果你第一次尝试从它创建一个数据表,你的更改将是
更高。

您还必须知道您使用的协议。标准将HTTP 1.1
不能同时处理超过2个连接(这是可调整的)。

因此,除了线程之外,还有许多其他问题要做。在没有线程的情况下首先制作
的程序可能是一个更好的方法。
添加比一些线程主要不是什么大挑战。

只是我的想法,

Cor



HK写道:

我不明白你的想法,我不会得到很多好处。我看到它不同但也许我错了。也许我的规格还不清楚。

除了HTTP连接限制外,我会从这里开始:
------------------ -----------
发送XML请求
在网络延迟后1秒钟获取XML响应
直到所有100,000条记录都用尽
- ----------------------------

对此:

--- --------------------------
如果线程可用

spawn任务与此记录
结束如果
直到所有100,000个记录都已完成

子任务
发送XML请求
在网络延迟后1秒获得XML响应
通信该线程是完成回到主回路
End Sub
-----------------------------

每项任务花费的大部分时间都在等待互联网延迟。如果我有10个工蜂(线程)都记录在记录集中的下一条记录,我每秒钟可能会有10个响应,而不是每秒响应1个,我可以通过我的100,000条记录快了10倍。带宽不是瓶颈。看起来像一个理想的线程应用程序,而另一端的公司已经同意。但是当然可以告诉我,如果我错了。

我只需要更好的伪代码,因为我不知道
如何跟踪可用的线程然后如何用下一个记录产生下一个


Cor Ligthert [MVP]" <无************ @ planet.nl>在消息中写道
新闻:O9 ************** @ TK2MSFTNGP09.phx.gbl ...

HK,
你得到带有记录集的线程样本的变化几乎是



到0,001%

也许如果你第一次尝试从中创建一个数据表,你的更改将



更高。

您还必须知道您使用的协议。标准将HTTP 1.1
不能同时处理超过2个连接(这是可调整的)。

因此,除了线程之外,还有许多其他问题要做。在没有线程的情况下首先制作



程序可能是一个更好的方法。



添加

比一些线程主要不是大挑战。

只是我的想法,

Cor




让我看看你是否理解你要做的事情:


你有100,000条记录在当地方面。对于每条记录,您必须要求b $ b发出网络请求。你认为你可以一次发出10个网页请求

以加快这个过程。


我明白为什么你认为线程可以加速这个,但是你可以更好地一次发送多个请求。我不确定你是否可以访问服务器端的东西,但是你应该设计你的xml到
在一次发送中处理多个请求。然后你发送100,000个请求

并且只有延迟一次。


克里斯


With VB.NET 2005, and a Windows Form, running on a dual CPU box, I need to
take a recordset (e.g. 100,000 records) and spawn a thread to handle an
internet XML transaction routine for each of the records. This is a nice
use of threading because those internet requests are going against 3rd party
servers that often have 1 second latency problems and so handling them with
multiple threads is the fastest way to get through all the records in the
recordset. I''d like to set a variable/constant that controls the number
of threads that are allowed to run simultaneously and have the code keep
spawning threads up to the number. Then, I can play with that number and
find out what # of threads works best. But I''m not the threading expert
and I don''t know what new options I have with .NET 2.0 framework. I also
can''t find any example that shows threads being repeatedly spawned up to a
certain number in an effort to work through a single recordset. Obviously
each thread need to communicate back to the main routine when done (fire an
event??) and that main routine would need to then take the next record and
throw it to another thread, if # threads is less than max # threads. Can
someone please help me with the pseudocode for this, or better yet, point me
to a real code example that does this?

解决方案

HK,

The change that you get a threading sample with a recordset is almost equal
to 0,001%

Maybe if you first try to make a datatable from it that your change will be
higher.

As well do you have to know which protocol you use. Standard will HTTP 1.1
not handle more than 2 connections at the same time (this is tweakable).

So beside the threading you have a lot of others problems to do. Making your
program first without threading will probably a much better approach. Adding
than some threading is mostly not the big challenge.

Just my thought,

Cor


I don''t understand your thinking that I won''t get much benefit. I see it
differently but maybe I''m wrong. Maybe my spec wasn''t clear.

HTTP connection limits aside, I would go FROM this:
-----------------------------
Do
send XML request
get XML response 1 second later after internet latency
Until All 100,000 Records Exhaused
-----------------------------

TO this:

-----------------------------
Do
if thread available
spawn Task with this record
end if
Until All 100,000 Records Exhaused

Sub Task
send XML request
get XML response 1 second later after internet latency
communicate that thread is finished back to main loop
End Sub
-----------------------------
Most of the time spent on each task is waiting for internet latency. If I
have 10 worker bees (threads) all taking the next record in the recordset, I
would probably have 10 responses every second rather than 1 response every
second and I could go through my 100,000 records 10x faster. Bandwidth is
not a bottleneck. Seems like an ideal threading app, and the companies on
the other end have agreed. But tell me if I''m wrong, of course.

I just need better pseudocode that what I wrote above because I don''t know
how to track what thread is available and how to then spawn off the next one
with the next record.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...

HK,

The change that you get a threading sample with a recordset is almost equal to 0,001%

Maybe if you first try to make a datatable from it that your change will be higher.

As well do you have to know which protocol you use. Standard will HTTP 1.1
not handle more than 2 connections at the same time (this is tweakable).

So beside the threading you have a lot of others problems to do. Making your program first without threading will probably a much better approach. Adding than some threading is mostly not the big challenge.

Just my thought,

Cor



HK wrote:

I don''t understand your thinking that I won''t get much benefit. I see it
differently but maybe I''m wrong. Maybe my spec wasn''t clear.

HTTP connection limits aside, I would go FROM this:
-----------------------------
Do
send XML request
get XML response 1 second later after internet latency
Until All 100,000 Records Exhaused
-----------------------------

TO this:

-----------------------------
Do
if thread available
spawn Task with this record
end if
Until All 100,000 Records Exhaused

Sub Task
send XML request
get XML response 1 second later after internet latency
communicate that thread is finished back to main loop
End Sub
-----------------------------
Most of the time spent on each task is waiting for internet latency. If I
have 10 worker bees (threads) all taking the next record in the recordset, I
would probably have 10 responses every second rather than 1 response every
second and I could go through my 100,000 records 10x faster. Bandwidth is
not a bottleneck. Seems like an ideal threading app, and the companies on
the other end have agreed. But tell me if I''m wrong, of course.

I just need better pseudocode that what I wrote above because I don''t know
how to track what thread is available and how to then spawn off the next one
with the next record.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...

HK,

The change that you get a threading sample with a recordset is almost



equal

to 0,001%

Maybe if you first try to make a datatable from it that your change will



be

higher.

As well do you have to know which protocol you use. Standard will HTTP 1.1
not handle more than 2 connections at the same time (this is tweakable).

So beside the threading you have a lot of others problems to do. Making



your

program first without threading will probably a much better approach.



Adding

than some threading is mostly not the big challenge.

Just my thought,

Cor




Let me see if I understand what you are trying to do:

You have 100,000 records on the local side. For each record you have to
make a web request. Your thinking you can make 10 web requests at once
in order to speed up the process.

Well I understand why you think threads would speed this up, but you''d
be better sending more than one request at a time. I''m not sure if you
have access to the server side stuff, but you should design your xml to
handle multiple requests in one send. Then you send in 100,000 requests
and only have the latency once.

Chris


这篇关于需要伪代码/维护X个线程的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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