Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8782625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:36:50+00:00 2026-06-13T20:36:50+00:00

I have a List with objects List<> my_objects . Several threads can change this

  • 0

I have a List with objects List<> my_objects. Several threads can change this list. Let’s suppose that I do not have any locking mechanism for controlling the access to the list.

What happens, if I’m working with an object of the list and suddenly another thread sets the array to null?

    MyObject o = my_objects[i];

    //now a second thread has set my_objects to null
    o.myMethod();

Will a NullPointerException occur?

Furthermore, what happens if a second thread removes the considered object from the list while I’m working with it in the first thread?

   //second thread:
   //y = i
   MyObject o2 = my_objects[y];

   my_objects.Remove(o2);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:36:51+00:00Added an answer on June 13, 2026 at 8:36 pm
    MyObject o = my_objects[i];
    
    //now a second thread has set my_objects to null, and/or invoked my_objects.Remove(o)
    o.myMethod();
    

    That’s fine. The variable my_objects holds references to objects, not the objects themselves. When you make a new variable o, it holds a second reference to the same object. Then you clear my_objects, which means you can’t refer to the actual object through my_objects anymore. But that doesn’t change the fact that you still have o, and you can still refer to the actual object through o. (And that in turn implies that the object itself still exists. C# is a garbage-collected language, which, to a first approximation means that objects are guaranteed to stick around until you’re done with them. Non-managed C++ is different in this respect, and Objective-C is different in yet a third way. Not that you asked.)

    Effectively, in this respect, references to MyObject behave just like primitive types like int. If I have an array of int, and I do

    int oi = my_ints[i];
    

    then I can do whatever I want with my_ints (clear it, remove items from it) and that won’t change the fact that oi still exists.

    The thing about reference types like MyObject, and the reason you got confused enough to ask this question, is that with MyObject both variables (o and my_objects[i]) still refer to the same actual object, so if you mutate the object via my_objects[i].gainSpiderPowers(), you’ll see the effects of that mutation no matter whether you’re looking via my_objects[i] or via o.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List<T> that is holding several objects called tc of type TwitterCollection
I have several objects that look like this: class PhoneNumber { String getNumber(); String
I have a list of objects (created from several text files) in C#.net that
I have a servlet with several methods that get a list of objects from
I have a form that returns me a List of FlatSessie objects in my
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have several templated objects that all implement the same interface: I.E. MyObject<datatype1> obj1;
I have a List collection of my objects. I need to display this collection
I have a list of objects (Foo). A Foo object has several attributes. An
I have these container objects (let's call them Container) in a list. Each of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.