线程和ArrayList访问 [英] Threading and ArrayList access

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

问题描述

我有一个关于线程和访问私有类

变量的问题。我正在开发一个Windows服务,里面有一个类,它的b $ b,它将通过

代表接收各种异步调用。


在类的一个方法(在我的

windows服务中是一个私有变量)中,通过在其列表中添加一个新的

项来修改ArrayList变量。每次通过异步

委托(来自众多来源)调用该方法时,都会向此ArrayList添加一个数字。

多个代表可能正在尝试同时调用

方法。


现在,我理解在需要时使用Monitor.Enter / Exit的概念

访问特定值或某事,但这需要吗?我只需在列表中添加项目即可获得
。我不在乎他们添加了b $ b的订单,我只是想确定它们已被添加。


我是否需要使用Monitor 。输入/退出,或者我可以简单地将项目添加为

通常吗?


提前致谢。


-

Mike
mi ********* @ hotmail.com

解决方案

Mike,


你绝对应该使用SyncLock或Monitor class来同步add

进程,否则你最终会有两个线程覆盖要设置的数组的相同

元素。如果第一个线程没有增加元素计数器,那么线程2将会是这种情况,而线程2就是将

对象分配给数组。 br />

Klaus


" Mike" < MI ********* @ hotmail.com> schrieb im Newsbeitrag

新闻:cb ************************** @ posting.google.c om ...

我有一个关于线程和访问私有类
变量的问题。我正在开发一个Windows服务,里面有一个类,它将通过
代表接收各种异步调用。

在类的一个方法里面(这是一个在我的
Windows服务中的私有变量),通过向其列表添加新的
项来修改ArrayList变量。每次通过异步
委托(来自众多来源)调用该方法时,都会向此ArrayList添加一个数字。
多个代表可能正在尝试调用
方法同时。

现在,我理解在需要访问特定值或某些东西时使用Monitor.Enter / Exit的概念,但这需要吗?我只是简单地将项目添加到列表中。我不在乎他们添加的订单,我只是想确定它们已被添加。

我是否需要使用Monitor.Enter / Exit,或者我可以只需将项目添加为平时?

提前致谢。

-
Mike
mi ********* @ hotmail.com



< blockquote> Mike,


你绝对应该使用SyncLock或Monitor类同步add

进程,否则你最终可能会有两个线程覆盖相同的

要设置的数组元素。如果第一个线程没有增加元素计数器,那么线程2将会是这种情况,而线程2就是将

对象分配给数组。 br />

Klaus


" Mike" < MI ********* @ hotmail.com> schrieb im Newsbeitrag

新闻:cb ************************** @ posting.google.c om ...

我有一个关于线程和访问私有类
变量的问题。我正在开发一个Windows服务,里面有一个类,它将通过
代表接收各种异步调用。

在类的一个方法里面(这是一个在我的
Windows服务中的私有变量),通过向其列表添加新的
项来修改ArrayList变量。每次通过异步
委托(来自众多来源)调用该方法时,都会向此ArrayList添加一个数字。
多个代表可能正在尝试调用
方法同时。

现在,我理解在需要访问特定值或某些东西时使用Monitor.Enter / Exit的概念,但这需要吗?我只是简单地将项目添加到列表中。我不在乎他们添加的订单,我只是想确定它们已被添加。

我是否需要使用Monitor.Enter / Exit,或者我可以只需将项目添加为平时?

提前致谢。

-
Mike
mi ********* @ hotmail.com



< blockquote>" Klaus L?ffelmann" < FO *********** @ loeffelmann.de>在留言新闻中写道:< ON ************* @ TK2MSFTNGP09.phx.gbl> ...

迈克,

你肯定应该使用SyncLock或Monitor类来同步add
进程,否则最终可能会有两个线程覆盖要设置的数组的相同元素。如果线程1还没有增加元素计数器,那么情况就是这样,线程2就要将
对象分配给数组。




感谢您的回复。


好​​吧,我将彻底解决SyncLock的问题,使类线程安全。

但它似乎我我不是在SyncLock上知道的,因为我认为我是
是..


如果我有这样的代码:


Public Class Foo

Private _someList As New ArrayList


Public Sub FooTest()

SyncLock _someList

_someList.Add(" foo test")

结束SyncLock

结束子


Public Sub FooTest2()

_someList.Clear()

结束子

结束班


如果我有多个线程调用Foo.FooTest(),同时也是

防止其他ArrayList方法在

SyncLock内发生吗?


如果是read已经锁定了FooTest中的_someList,而其他一些线程已经被称为Foo.FooTest2(),调用FooTest2的线程会被阻塞

直到第一个线程通过End释放_someList SyncLock?


谢谢。


-

Mike
mi ********* @ hotmail.com


I had a question about threading and access to private class
variables. I am developing a windows service which has a class inside
of it which will receive various asynchronous calls to it via
delegates.

Inside one method of the class (which is a private variable in my
windows service), an ArrayList variable is modified by adding a new
item to its list. Each time the method is called via an asynchronous
delegate (from numerous sources), a number is added to this ArrayList.
It is possible that multiple delegates may be trying to call the
method at the same time.

Now, I understand the concept of using Monitor.Enter/Exit when needing
access to specific values or something, but is that needed here? I am
simply adding items to the list. I don''t care about the order they
are added in, I simply want to be sure they are added.

Do I need to use Monitor.Enter/Exit, or can I simply add the item as
usual?

Thanks in advance.

--
Mike
mi*********@hotmail.com

解决方案

Mike,

you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same
element of the array to set. That would be the case, if thread one hasn''t
yet increased the element counter, and thread 2 is just about to assign the
object to the array.

Klaus

"Mike" <mi*********@hotmail.com> schrieb im Newsbeitrag
news:cb**************************@posting.google.c om...

I had a question about threading and access to private class
variables. I am developing a windows service which has a class inside
of it which will receive various asynchronous calls to it via
delegates.

Inside one method of the class (which is a private variable in my
windows service), an ArrayList variable is modified by adding a new
item to its list. Each time the method is called via an asynchronous
delegate (from numerous sources), a number is added to this ArrayList.
It is possible that multiple delegates may be trying to call the
method at the same time.

Now, I understand the concept of using Monitor.Enter/Exit when needing
access to specific values or something, but is that needed here? I am
simply adding items to the list. I don''t care about the order they
are added in, I simply want to be sure they are added.

Do I need to use Monitor.Enter/Exit, or can I simply add the item as
usual?

Thanks in advance.

--
Mike
mi*********@hotmail.com



Mike,

you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same
element of the array to set. That would be the case, if thread one hasn''t
yet increased the element counter, and thread 2 is just about to assign the
object to the array.

Klaus

"Mike" <mi*********@hotmail.com> schrieb im Newsbeitrag
news:cb**************************@posting.google.c om...

I had a question about threading and access to private class
variables. I am developing a windows service which has a class inside
of it which will receive various asynchronous calls to it via
delegates.

Inside one method of the class (which is a private variable in my
windows service), an ArrayList variable is modified by adding a new
item to its list. Each time the method is called via an asynchronous
delegate (from numerous sources), a number is added to this ArrayList.
It is possible that multiple delegates may be trying to call the
method at the same time.

Now, I understand the concept of using Monitor.Enter/Exit when needing
access to specific values or something, but is that needed here? I am
simply adding items to the list. I don''t care about the order they
are added in, I simply want to be sure they are added.

Do I need to use Monitor.Enter/Exit, or can I simply add the item as
usual?

Thanks in advance.

--
Mike
mi*********@hotmail.com



"Klaus L?ffelmann" <fo***********@loeffelmann.de> wrote in message news:<ON*************@TK2MSFTNGP09.phx.gbl>...

Mike,

you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same
element of the array to set. That would be the case, if thread one hasn''t
yet increased the element counter, and thread 2 is just about to assign the
object to the array.



Thanks for the reply.

Well, I will deifnitely SyncLock things to make the class thread safe.
But it appears I am not as knowledgable on SyncLock as I thought I
was..

If I have code like this:

Public Class Foo
Private _someList As New ArrayList

Public Sub FooTest()
SyncLock _someList
_someList.Add("foo test")
End SyncLock
End Sub

Public Sub FooTest2()
_someList.Clear()
End Sub
End Class

If I have multiple threads call Foo.FooTest(), does the synclock also
protect against other ArrayList methods occuring while inside the
SyncLock?

If a thread had locked _someList in FooTest, and some other thread had
called Foo.FooTest2(), would the thread calling FooTest2 be blocked
until the first thread released _someList via End SyncLock?

Thanks.

--
Mike
mi*********@hotmail.com


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

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