问题邻接线程 [英] Question abut threads

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

问题描述

我正在开发一个程序,它创建一些TCP侦听器来接受来自非.net进程的数据$

每个侦听器都在自己的线程中侦听。我正在使用Thread.Sleep(1000)

让其他线程工作。

这是我的代码:

tcpl = new TcpListener(ipe) );

TcpClient tcpc = tcpl.AcceptTcpClient();

while(true)

{

//获取数据

while(true)

{

//如果contidion遇到休息;

}

Thread.Sleep(1000);

}


我真的需要Thread.Sleep(1000)吗?如果我不在

全部使用它会发生什么?


谢谢

I am working on a program that creates a few TCP listenrs to accept data
from non .net processes.
Each listener listens in its own thread. I am using Thread.Sleep(1000) to
let other threads work.
Here is my code:
tcpl = new TcpListener(ipe);
TcpClient tcpc = tcpl.AcceptTcpClient();
while (true)
{
// get data
while (true)
{
// if contidion met break;
}
Thread.Sleep(1000) ;
}

Do I really need Thread.Sleep(1000) ? What would happend if I dont use it at
all?

Thanks

推荐答案

8月7日,1:25 * pm,Markgoldin。 < markgoldin_2 ... @ yahoo.comwrote:
On Aug 7, 1:25*pm, "Markgoldin" <markgoldin_2...@yahoo.comwrote:

我正在开发一个程序,创建一些TCP侦听器来接受数据

来自非.net进程。

每个侦听器都在自己的线程中侦听。我正在使用Thread.Sleep(1000)

让其他线程工作。

这是我的代码:

tcpl = new TcpListener(ipe) );

TcpClient tcpc = tcpl.AcceptTcpClient();

while(true)

{

* * //获取数据

* * while(true)

* * {

* * * * //如果contidion遇到休息;

* *}

* * Thread.Sleep(1000);


}


我真的需要Thread.Sleep(1000)吗?如果我不使用它会发生什么

all?
I am working on a program that creates a few TCP listenrs to accept data
from non .net processes.
Each listener listens in its own thread. I am using Thread.Sleep(1000) to
let other threads work.
Here is my code:
tcpl = new TcpListener(ipe);
TcpClient tcpc = tcpl.AcceptTcpClient();
while (true)
{
* * // get data
* * while (true)
* * {
* * * * // if contidion met break;
* * }
* * Thread.Sleep(1000) ;

}

Do I really need Thread.Sleep(1000) ? What would happend if I dont use itat
all?



这取决于循环中发生的事情。你真的有可能会受到处理器限制吗?你的while循环是一个紧密的循环吗?如果是这样,那就是

要修复的东西。


你不太可能真正睡觉,但是如果你需要的话br />
等待条件得到满足,你应该使用

Auto / ManualResetEvent或Monitor.Wait / Pulse。


Jon

That depends on what happens in the loop. Are you actually likely to
be processor bound? Is your while loop a tight loop? If so, that''s the
thing to fix.

It''s unlikely that you should really be sleeping, but if you ever need
to wait for a condition to be met, you should probably be using an
Auto/ManualResetEvent or Monitor.Wait/Pulse.

Jon


8月7日,8:25 * am,Markgoldin < markgoldin_2 ... @ yahoo.comwrote:
On Aug 7, 8:25*am, "Markgoldin" <markgoldin_2...@yahoo.comwrote:

我正在开发一个程序,创建一些TCP侦听器来接受数据

来自非.net进程。

每个侦听器都在自己的线程中侦听。我使用Thread.Sleep(1000)

让其他线程工作。
I am working on a program that creates a few TCP listenrs to accept data
from non .net processes.
Each listener listens in its own thread. I am using Thread.Sleep(1000) to
let other threads work.



如果每个听众都听不同的帖子,你应该这样做。

you should only do this if each listener listen in a different post.


这是我的代码:

tcpl = new TcpListener(ipe);

TcpClient tcpc = tcpl.AcceptTcpClient();

while(true)

{

* * //获取数据

* * while(true)

* * {

* * * * //如果contidion遇到休息;

* *}

* * Thread.Sleep(1000);
< br $>
}


我真的需要Thread.Sleep(1000)吗?如果我不使用它会发生什么

all?
Here is my code:
tcpl = new TcpListener(ipe);
TcpClient tcpc = tcpl.AcceptTcpClient();
while (true)
{
* * // get data
* * while (true)
* * {
* * * * // if contidion met break;
* * }
* * Thread.Sleep(1000) ;

}

Do I really need Thread.Sleep(1000) ? What would happend if I dont use itat
all?



为什么你首先使用它?


回答这个问题,你使用的是一个端口还是多个端口?

我可以在每种情况下给你代码,就像使用SyncQueue一样简单。

和每个连接一个线程(每个监听器没有)

why you use it in the first place??

Answer to this, are you using one port or multiple ports?
I can give you code in each case, it''s as simple as using a SyncQueue.
and one thread per connection (no per listener)


2008年8月7日星期四08:04:03 -0700,Ignacio Machin(.NET / C#MVP)

< ig **** ********@gmail.com写道:
On Thu, 07 Aug 2008 08:04:03 -0700, Ignacio Machin ( .NET/ C# MVP )
<ig************@gmail.comwrote:

8月7日,8:25?* am,Markgoldin < markgoldin_2 ... @ yahoo.comwrote:
On Aug 7, 8:25?*am, "Markgoldin" <markgoldin_2...@yahoo.comwrote:

>我正在开发一个程序,创建一些TCP侦听器来接受来自非的数据
.net进程。
每个侦听器都在自己的线程中侦听。我正在使用Thread.Sleep(1000)
让其他线程工作。
>I am working on a program that creates a few TCP listenrs to accept data
from non .net processes.
Each listener listens in its own thread. I am using Thread.Sleep(1000)
to
let other threads work.



如果每个听众都听不同的帖子,你应该这样做。


you should only do this if each listener listen in a different post.



我相信Ignacio在这里有一个单字母类型:post应该是port。

希望从其余的消息中可以清楚地看到,但如果不是这个,那么

应该清除它。 :)

I believe Ignacio has a one-letter type here: "post" should be "port".
Hopefully that was clear from the rest of his message, but if not this
should clear it up. :)


[...]
[...]

>我真的需要Thread.Sleep(1000 )?如果我不使用
它会发生什么?
>Do I really need Thread.Sleep(1000) ? What would happend if I dont use
it at
all?



为什么你首先使用它?


回答这个问题,你使用的是一个端口还是多个端口?

我可以在每种情况下给你代码,它就像使用SyncQueue一样简单。

和每个连接一个线程(每个监听器没有)


why you use it in the first place??

Answer to this, are you using one port or multiple ports?
I can give you code in each case, it''s as simple as using a SyncQueue.
and one thread per connection (no per listener)



我找不到类型SyncQueue在MSDN文档中,所以我不会知道Ignacio会得到什么。也就是说,我会建议在.NET中只有两种可行的TCP网络i / o方法:


- 一个线程每个连接,使用阻塞i / o

- 异步API(例如Socket.BeginReceive()等)


(对于i / o使用直接使用Socket类,有一个Select()方法

类似于BSD套接字的select()函数,理论上比异步API更简单
,但我个人而言认为它既尴尬又无效。

效率低下..NET有更好的i / o范例可以利用)。


前者可以是最简单的,假设每个连接完全相互独立。但它只适用于相对较少的远程端点(数百个,最多)。

后者更具可扩展性,更重要的是,除了每个连接一个线程之外,大多数实现都是b $ b。设计将结束

无论如何涉及一些类似的线程管理问题。还有一个人可能会使用内置的线程i / o而不是重新发明轮子。


MSDN在异步API中都有代码示例套接字类

,如果我没记错的话,还有TcpListener / TcpClient类。


Pete

I don''t find the type "SyncQueue" in the MSDN documentation, so I don''t
know what Ignacio is getting at. That said, I''ll suggest that there are
really only two viable approaches to TCP network i/o in .NET:

-- one thread per connection, using blocking i/o
-- asynchronous API (e.g. Socket.BeginReceive(), etc.)

(For i/o using the Socket class directly, there is a Select() method
similar to BSD sockets''s select() function which is theoretically simpler
than the asynchronous API, but personally I think it''s both awkward and
inefficient. .NET has much better i/o paradigms to take advantage of).

The former can be the simplest, assuming each connection is completely
independent from each other. But it''s only appropriate for relatively
small numbers of remote endpoints (hundreds, at the very most). The
latter is much more scalable, and more importantly, most implementations
other than the "one thread per connection" design are going to wind up
involving some similar thread management issues anyway. One might as well
use the built-in threaded i/o rather than reinventing the wheel.

MSDN has code samples for the asynchronous API both in the Socket class
and, if I recall correctly, the TcpListener/TcpClient classes.

Pete


这篇关于问题邻接线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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