C#中的多线程 [英] multi threading in C#

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

问题描述

大家好,


我刚开始用C#学习线程。我阅读了很多文章以及我想要在这里完成的工作是什么?


我有一个字符串数组,我想通过该成员数组

逐个到每个线程直到处理完成。

如何将值一个接一个地传递给myThreadprocess1并且只要一个

线程完成了这个过程,

它应该从数组中获取另一个项目并处理该项目。我怎么能这样做?
呢?以下是我的代码:


private void processTest()

{


string [] x = {1234 ,6789,35678,6789,023456};

AutoResetEvent MyThread1 = new AutoResetEvent(false);

AutoResetEvent MyThread2 = new AutoResetEvent(false);


ThreadPool.QueueUserWorkItem(new WaitCallback(MyThreadProcess1,

x [0]),MyThread1);

ThreadPool.QueueUserWorkItem(new WaitCallback(MyThreadProcess2,
x [1]),MyThread2);


MyThread1.WaitOne();

MyThread2.WaitOne();

}


静态无效MyThreadProcess1(对象状态)

{

}

Hello Everyone,

I just started learning Threading in C#. I read lot of articles and what I
am trying to accomplish here is

that I have an array of string and I want to pass the member of that array
to each thread one by one until the processing is done.
how can I pass the values one by one to myThreadprocess1 and as soon as one
thread is done with the process,
it should grab another item from array and process that item. How can I
accomplish this? Below is my code:

private void processTest()
{

string[] x = { 1234, 6789, 35678, 6789, 023456};
AutoResetEvent MyThread1 = new AutoResetEvent(false);
AutoResetEvent MyThread2 = new AutoResetEvent(false);

ThreadPool.QueueUserWorkItem(new WaitCallback(MyThreadProcess1,
x[0]), MyThread1);
ThreadPool.QueueUserWorkItem(new WaitCallback(MyThreadProcess2,
x[1]), MyThread2);

MyThread1.WaitOne();
MyThread2.WaitOne();
}

static void MyThreadProcess1(Object state)
{
}

推荐答案

Vinki< Vi *** @ discussion.microsoft.comwrote:
Vinki <Vi***@discussions.microsoft.comwrote:

我刚开始学习C#中的线程。我阅读了很多文章以及我想要在这里完成的工作是什么?


我有一个字符串数组,我想通过该成员数组

逐个到每个线程直到处理完成。

如何将值一个接一个地传递给myThreadprocess1并且只要一个

线程完成了这个过程,

它应该从数组中获取另一个项目并处理该项目。我怎么能这样做?
呢?下面是我的代码:
I just started learning Threading in C#. I read lot of articles and what I
am trying to accomplish here is

that I have an array of string and I want to pass the member of that array
to each thread one by one until the processing is done.
how can I pass the values one by one to myThreadprocess1 and as soon as one
thread is done with the process,
it should grab another item from array and process that item. How can I
accomplish this? Below is my code:



听起来你想要一个生产者/消费者队列,或类似

的东西。请参阅本页的后半部分:

http://pobox.com/~skeet/csharp/threads/deadlocks.shtml

-

Jon Skeet - < sk***@pobox.com>
http://www.pobox。 com / ~siget 博客: http://www.msmvps.com/ jon.skeet

如果回复该团体,请不要给我发邮件

It sounds like you want a producer/consumer queue, or something like
it. See the second half of this page:

http://pobox.com/~skeet/csharp/threads/deadlocks.shtml

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


2007年5月23日星期三14: 57:01 -0700,Vinki

< Vi *** @ discussion.microsoft.comwrote:
On Wed, 23 May 2007 14:57:01 -0700, Vinki
<Vi***@discussions.microsoft.comwrote:

我刚刚开始学习线程C#。我读了很多文章和

我想在这里完成的是


我有一个字符串数组,我想通过那个成员

数组

逐个到每个线程直到处理完成。

如何将值逐个​​传递给myThreadprocess1和as很快

一个

线程完成了这个过程,

它应该从数组中获取另一个项目并处理该项目。我怎么能这样做?
呢? [...]
I just started learning Threading in C#. I read lot of articles and
what I am trying to accomplish here is

that I have an array of string and I want to pass the member of that
array
to each thread one by one until the processing is done.
how can I pass the values one by one to myThreadprocess1 and as soon as
one
thread is done with the process,
it should grab another item from array and process that item. How can I
accomplish this? [...]



除了Jon写的内容(你一定要读他的网页

文章),我不得不问你可以澄清你的目标。


我同意你似乎有一种生产者/消费者设置。但是它好像你只有一个生产者,并且取决于你的意思

"将该数组的成员逐个传递给每个线程,直到

处理完成,甚至可能只有一个消费者。


但是,假设你真的想让多个线程工作,那么所有

,当他们完成了他们正在处理的那个

之后从你的数组中获取一个新值,那么你有一个生产者/消费者场景,其中一个是
生产者(或者可能只是一组初始数据)和多个消费者

(处理数据的每个线程都是消费者)。使用Jon的

生产者/消费者代码作为起点,您将使用Produce()方法将

字符串数组中的元素添加到队列中,并且拥有尽可能多的

线程,而不是仅仅是他在他的示例代码中显示的那个(每个使用Consume()来获取新值工作)。


当然,请记住,要从多线程中获得真正的好处,

你需要在你的计算机上运行拥有尽可能多的CPU和

您有解决问题的线程,或者您有一些处理

主要等待I / O并希望确保您始终拥有一个线程

准备接受新的i / o发生时。只是在

抛出更多线程一些随机问题并不一定会加快速度,并且在

中,许多情况下实际上可能会减慢速度。

Pete

In addition to what Jon wrote (you should definitely read his web
article), I have to ask if you can clarify your goal.

I agree that you seem to have a sort-of producer/consumer setup. But it
seems like you only have one producer, and depending on what you mean by
"pass the member of that array to each thread one by one until the
processing is done", maybe even only one consumer.

But, assuming you really mean to have multiple threads working, all of
which pull a new value from your array when they''ve finished the one
they''re working on, then you have a producer/consumer scenario with one
producer (or possibly just an initial set of data) and multiple consumers
(each thread working on the data is a consumer). Using Jon''s
producer/consumer code as a starting point, you''d add elements from your
string array to the queue with the Produce() method, and have as many
threads as you want working on them instead of just the one he shows in
his example code (each using Consume() to get a new value to work on).

Keeping in mind, of course, that to get true benefits from multithreading,
you either need to be running on a computer where you have as many CPUs as
you have threads working on the problem, or you have some processing that
is mostly waiting on i/o and want to make sure you always have a thread
ready to accept new i/o when it occurs. Just throwing more threads at
some random problem is not necessarily going to speed things up, and in
many cases can actually slow things down.

Pete


2007年5月23日星期三15:08:33 -0700,Jon Skeet [C#MVP]< sk *** @ pobox。 com>

写道:
On Wed, 23 May 2007 15:08:33 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:

听起来你想要一个生产者/消费者队列,或类似

它。请参阅本页的后半部分:

http://pobox.com/~skeet/csharp/threads/deadlocks.shtml



顺便说一句,只要我们是关于这个话题,我已经好奇了一段时间。我在你的示例代码中看到,我已经在其他地方看到了这个,b / b
的新基础对象实例的创建用作锁定对象。我很好奇这是什么意思,因为

与简单地获取对象的锁定相比,你试图将b $ b同步访问。


例如,而不是使用listLock而不是对于lock()和Monitor

方法,为什么不使用queue?实例?


唯一想到的是,在某些情况下,你可能有一个

对象,其中只有某些部分需要同步,但那些

部件可能仍然不止是较大物体中的单个物体。在这种情况下,具有用于锁定的专用对象实例允许

将锁定语义与实际实现分开。


但是否则呢?这样做是否有其他充分的理由,或者只是为了保持一致,因为你需要这样做

有时候?


谢谢,

Pete

By the way, as long as we''re on the topic, there''s been something I''ve
been curious about for awhile. I see in your example code, and I''ve seen
this elsewhere as well, the creation of a new, base object instance for
use as a locking object. I am curious what the point of that is, as
compared to simply acquiring a lock on the object you are trying to
synchronize access to.

For example, instead of using "listLock" for the lock() and Monitor
methods, why not just use the "queue" instance?

The only thing that comes to mind is that in some cases, you may have an
object where only certain parts of it need to be synchronized, but those
parts may still be more than a single object within the larger object. In
that case, having a dedicated object instance for locking allows for
separation of the locking semantics from the actual implementation.

But otherwise? Are there other good reasons for doing it that way, or is
it just a matter of being consistent since you would need do it that way
sometimes?

Thanks,
Pete


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

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