473,324 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

thread safety and syncroot

TS

ArrayList myCollection = new ArrayList();
foreach ( Object item in myCollection ) {
// code here.
}

Can someone explain how more than one thread would execute code? So if i
have some code in a web page that a user runs that iterates over an
arraylist, what is the scenario where more than one thread would be running
this code and could possibly modify the collection before the iteration of
the arraylist completes?

Since i instantiate myCollection (only one thread always runs the code for a
single request?) how would any other thread access myCollection?

thanks
Feb 1 '07 #1
7 2854
n!
Since i instantiate myCollection (only one thread always runs the code for
a single request?) how would any other thread access myCollection?
It kinda depends on what goes in place of "// code here". Assuming it is
something simple and not passing out the collection somewhere else. Then no,
multiple threads should not be accessing the collection. However, by the
same assumption:
ArrayList myCollection = new ArrayList();
foreach ( Object item in myCollection ) {
// code here.
}
the collection is empty and thus nothing happens anyway. So do you have a
more complete example? The devil is in the details :)

n!
Feb 1 '07 #2
TS <ma**********@nospam.nospamwrote:
>
ArrayList myCollection = new ArrayList();
foreach ( Object item in myCollection ) {
// code here.
}

Can someone explain how more than one thread would execute code? So if i
have some code in a web page that a user runs that iterates over an
arraylist, what is the scenario where more than one thread would be running
this code and could possibly modify the collection before the iteration of
the arraylist completes?

Since i instantiate myCollection (only one thread always runs the code for a
single request?) how would any other thread access myCollection?
If you're creating the collection in a thread and never making it
available to any other thread, then you don't need to worry.

--
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
Feb 1 '07 #3
Hi,

"TS" <ma**********@nospam.nospamwrote in message
news:uo**************@TK2MSFTNGP03.phx.gbl...
|
| ArrayList myCollection = new ArrayList();
| foreach ( Object item in myCollection ) {
| // code here.
| }
|
| Can someone explain how more than one thread would execute code? So if i
| have some code in a web page that a user runs that iterates over an
| arraylist, what is the scenario where more than one thread would be
running
| this code and could possibly modify the collection before the iteration
of
| the arraylist completes?
|
| Since i instantiate myCollection (only one thread always runs the code for
a
| single request?) how would any other thread access myCollection?

The code below does not makes too much sense, you create the collection and
right away iterate in it without adding elements first. So I thnk the code
above is not the "real thing"

-Where is the collection created?

- How it's populated?
If the collection is used by only one thread (it's created & populate bu the
thread method) you are ok.

--
Ignacio Machin
machin AT laceupsolutions com
Feb 1 '07 #4
Besides,

Where is multi threading party going on?

--
Regards,
Robson Siqueira
Enterprise Architect
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:eV**************@TK2MSFTNGP06.phx.gbl...
Hi,

"TS" <ma**********@nospam.nospamwrote in message
news:uo**************@TK2MSFTNGP03.phx.gbl...
|
| ArrayList myCollection = new ArrayList();
| foreach ( Object item in myCollection ) {
| // code here.
| }
|
| Can someone explain how more than one thread would execute code? So if i
| have some code in a web page that a user runs that iterates over an
| arraylist, what is the scenario where more than one thread would be
running
| this code and could possibly modify the collection before the iteration
of
| the arraylist completes?
|
| Since i instantiate myCollection (only one thread always runs the code
for
a
| single request?) how would any other thread access myCollection?

The code below does not makes too much sense, you create the collection
and
right away iterate in it without adding elements first. So I thnk the code
above is not the "real thing"

-Where is the collection created?

- How it's populated?
If the collection is used by only one thread (it's created & populate bu
the
thread method) you are ok.

--
Ignacio Machin
machin AT laceupsolutions com


Feb 1 '07 #5
TS
sorry, for the example, i just copied it out of msdn. You should assume that
myCollection has been populated somewhere.

As for multiple threads accessing it, what are some scenarios when i'm
writing my code that i would enlist the help of additional threads (with me
knowing or not knowing)?

the only thing i think think of is performing an asynchronous operation.

thanks Jon!

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
TS <ma**********@nospam.nospamwrote:
>>
ArrayList myCollection = new ArrayList();
foreach ( Object item in myCollection ) {
// code here.
}

Can someone explain how more than one thread would execute code? So if i
have some code in a web page that a user runs that iterates over an
arraylist, what is the scenario where more than one thread would be
running
this code and could possibly modify the collection before the iteration
of
the arraylist completes?

Since i instantiate myCollection (only one thread always runs the code
for a
single request?) how would any other thread access myCollection?

If you're creating the collection in a thread and never making it
available to any other thread, then you don't need to worry.

--
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

Feb 2 '07 #6
Hello,

An ArrayList can support multiple readers concurrently, as long as the
collection is not modified. To guarantee the thread safety of the
ArrayList, all operations must be done through the wrapper returned by the
Synchronized method.

Enumerating through a collection is intrinsically not a thread-safe
procedure. Even when a collection is synchronized, other threads can still
modify the collection, which causes the enumerator to throw an exception.
To guarantee thread safety during enumeration, you can either lock the
collection during the entire enumeration or catch the exceptions resulting
from changes made by other threads.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 5 '07 #7
Hi,

"Robson Siqueira" <ro****@robsonfelix.comwrote in message
news:uy**************@TK2MSFTNGP05.phx.gbl...
| Besides,
|
| Where is multi threading party going on?

What u mean?
Feb 5 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Support | last post by:
This doubt is regarding synchronisation question in Singleton pattern code of C# I had created a class as public sealed class SecuriteManager { private static volatile SecurityManager...
10
by: Nikola Skoric | last post by:
Hello there, Lets say I have something like this in my Mother Of All Threads: Thread thread1 = new Thread(new ThreadStart(this.run)); thread1.Start(); //some unimportant code...
4
by: Anders Borum | last post by:
Hello! I'm am currently working on making a central cache component threadsafe, and was looking at the synchronized implementation of the Hashtable. I was wondering why you'd really want to...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
5
by: Stephane | last post by:
Hi, I want to keep a list of my visitors in an ArrayList which I place in the application object like this: Application("Visitors") = new ArrayList(); // The list of visitors Then, each...
7
by: Mythran | last post by:
Been 11 days since I posted this and 0 replies (although, in OE, it looks like there was 1 but it's just another post with the same subject as before): Part #1: I have a Thread, MainThread, and...
5
by: admin | last post by:
ok This is my main. Pretty much it goes through each category and starts up 4 worker threads that then ask for groups to gether from. My problem is that when the thread gets done it keeps the...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
12
by: Ronny | last post by:
Thanks Chris, Looks nice but I miss the dual way communication. In the main thread to deliver paramters and data to the worker thread- how can I do that? Regards Ronny Take a look at the...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.