重用线程(紧急) [英] reuse of threads (urgent)

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

问题描述



我想知道如何重用一个线程的对象(如果可能的话)

在Csharp中?我有以下程序:


使用System;

使用System.Threading;

使用System.Collections;


公共类A

{

私有线程;

私有静态队列q =新队列() ;

public A()

{

thread = new Thread(new ThreadStart(this.Run));

}


private static void getThread(A a)

{

if(a.thread!= null)

返回;

其他

if(q.count> 0)

//有可用的线程

a.thread =(Thread)q。Dequeue();

// **这里我必须告诉a.thread在

它开始**

//我不知道怎么做。

else

//制作一个新的线程对象

a.thread = new Thread(新的ThreadStart(a.Run));

}


private void Run ()

{

//做一些工作

thread.Sleep(1000);

//将线程objekt插入队列

q.Enqueue(thread);

thread = null;

}

} // A


这个实现不起作用,因为当一个线程被分配给一个A类
的A类新对象时它有知道要执行哪种方法。我知道我可以使用一个线程池,但是我不想这样做,因为我的程序(一个模板)有时会使用数百万个线程。 Threadpool默认只允许25

个线程。这是一个很难改变这个数字在

Csharp。


非常感谢提前:)


问候来自

Kovan

Hi,
I would like to know how to reuse an object of a thread (if it is possible)
in Csharp? I have the following program:

using System;
using System.Threading;
using System.Collections;

public class A
{
private Thread thread;
private static Queue q =new Queue();
public A()
{
thread = new Thread(new ThreadStart(this.Run));
}

private static void getThread(A a)
{
if (a.thread != null)
return;
else
if(q.count > 0)
// there are available threads
a.thread = (Thread) q.Dequeue();
// ** Here i have to tell a.thread to run a.Run method when
it starts **
// I do not know how to do that.
else
// make a new thread object
a.thread = new Thread(new ThreadStart(a.Run));
}

private void Run()
{
// do some work
thread.Sleep(1000);
//insert the thread objekt into a queue
q.Enqueue(thread);
thread = null;
}
} // A

This implementation does not work, because when a thread is assigned to a
new object of class A it has to know which method to execute. I know that I
could use a threadpool, but I do not want to because my program ( a
simulation) uses sometimes millions of threads. Threadpool allows only 25
threads by default. It is some how difficult to change this number in
Csharp.

Many thanks in advance :)

Regards from
Kovan

推荐答案

Kovan Akrei< ko **** @ ifi.uio.no>写道:
Kovan Akrei <ko****@ifi.uio.no> wrote:
我想知道如何重用一个线程的对象(如果有可能)
在Csharp?
I would like to know how to reuse an object of a thread (if it is possible)
in Csharp?




写一个系统,每个线程都知道一个工作项列表,并且
从列表中取出项目,直到列表为空,然后等待

列表到已添加工作项。这基本上就是ThreadPool中内置的

所做的。


你应该知道,当你说你打算使用百万时

个主题这实际上不会发生 - 即使它确实发生了,

的表现也会令人难以忍受。


-

Jon Skeet - < sk *** @ pobox.com>
http: //www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Write a system where each thread knows about a list of work items, and
takes items off the list until the list is empty, then waits for the
list to have work items added to it. This is basically what the built-
in ThreadPool does anyway.

You should be aware that when you say you''re going to use "millions of
threads" that''s not actually going to happen - and even if it did,
performance would be excruciating.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


它还应该注意,ThreadPool将允许您根据需要创建多个^ b $ b多个^请求^。它只是不会执行超过25个线程

(这是当前为每个处理器分配的数字,但需要更改

)。


因此,在这种情况下,ThreadPool实际上将适合原始的

海报的需要,因为它将排队所有工作,然后执行时它

可以。就像Jon说的那样,你无法在同一时间获得100万个b $ b线程。如果有的话,我打赌使用

的性能线程池会在创建一百万个b / b
线程时破坏性能。我认为单独的上下文切换会杀了你。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
It should also be noted that the ThreadPool will allow you to create as
many ^requests^ as you wish. It just won''t execute more than 25 threads
(this is the number currently assigned per processor, but is subject to
change).

So, in this situation, the ThreadPool will actually suit the original
poster''s needs, as it will queue up all the work, and then execute when it
can. Like Jon said, there is no way you are going to get one million
threads to run at the same time. If anything, I bet the performance using
the thread pool would destroy the performance when creating one million
threads. I would think the context switches alone would kill you.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kovan Akrei< ko **** @ ifi.uio.no>写道:
Kovan Akrei <ko****@ifi.uio.no> wrote:
我想知道如何在Csharp中重用一个线程的对象(如果它是
)?
I would like to know how to reuse an object of a thread (if it is possible) in Csharp?



写一个系统,其中每个线程知道工作项列表,并从列表中取出项目,直到列表为空,然后等待
列表添加工作项。这基本上就是ThreadPool中的内置功能。<​​br />
当你说你要使用数百万个线程时,你应该知道。这实际上并不会发生 - 即使它确实如此,
表现也会令人难以忍受。

-
Jon Skeet - < sk *** @ pobox .com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



Write a system where each thread knows about a list of work items, and
takes items off the list until the list is empty, then waits for the
list to have work items added to it. This is basically what the built-
in ThreadPool does anyway.

You should be aware that when you say you''re going to use "millions of
threads" that''s not actually going to happen - and even if it did,
performance would be excruciating.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



感谢您的回复如此之快:)


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
Thanks for replying so fast :)

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kovan Akrei< ko **** @ ifi.uio.no>写道:
Kovan Akrei <ko****@ifi.uio.no> wrote:
我想知道如何在Csharp中重用一个线程的对象(如果它是
)?
写一个系统,其中每个线程都知道一个列表工作项目,
从列表中取出项目,直到列表为空,然后等待
列表添加工作项目。这基本上就是ThreadPool中内置的
I would like to know how to reuse an object of a thread (if it is possible) in Csharp?
Write a system where each thread knows about a list of work items, and
takes items off the list until the list is empty, then waits for the
list to have work items added to it. This is basically what the built-
in ThreadPool does anyway.



你所说的列表在模拟中会很快变空

程序。我试图避免每次创建新进程时创建新线程即将创建模拟中的
对象。通过这样做,我将避免分配额外费用。在我的模拟程序(离散事件模拟)中,我有时需要运行一百个线程,但并非所有线程都可以同时激活
。其中一些处于WaitSleep状态,而另外一些处于运行状态(使用cpu功率)。在运行状态下,差不多有25个线程。线程池的问题是,当一个程序耗尽了池其他进程必须执行时,


一次必须等到有一个线程可用线程池。

你应该知道,当你说你要使用数百万的线程时这实际上不会发生 - 即使它确实发生了,
表现也会令人难以忍受。


The list you are talking about will get empty very fast in a simulation
program. Im trying to avoid creating new threads each time a new process
object in the simulation is about to be created. By doing so I''ll avoid
allot of overhead. In my simulation program (discrete event simulation) I
get sometimes up to one hundred thousen threads running, but not all of them
are active at the same time. Some of them are in WaitSleep state, while
others are in running state (using cpu power). There are almost alyways more
than 25 threads in a running state. The problem with a thread pool is that
when a program has exhausted the pool other process which has to be executed
at once has to wait until there is a thread available in threadpool.
You should be aware that when you say you''re going to use "millions of
threads" that''s not actually going to happen - and even if it did,
performance would be excruciating.



我知道。正如我先前所说。并非所有人都处于不稳定状态,但

其中很多都是。


Kovan


I know. As I stated earlier. Not all of them are in a rrunning state, but
many of them are.

Kovan


这篇关于重用线程(紧急)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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