I want 500 objects to be processed concurrently using multithreading. These objects will have some initial data before the threads are created.

I am creating 5 threads and each thread takes 100 objects and starts the operation.

Here is the code.

public class InputData
   public string initialData {get; set;}
   public string formula {get; set;}
   public string resultData {get; set;}

public static List<string> result = new List<string>(500);

public static void Main()
    List<InputData> lst = new List<InputData>(500);

    List<InputData> lst_1 = .. // This will hold 1 to 100 objects
    List<InputData> lst_2 = .. // This will hold 101 to 200 objects
    List<InputData> lst_3 = .. // This will hold 201 to 300 objects
    List<InputData> lst_4 = .. // This will hold 301 to 400 objects
    List<InputData> lst_5 = .. // This will hold 401 to 500 objects

    Thread t1 = new Thread(Update);
    Thread t5 = new Thread(Update);


    //while condition to wait for the threads to finish processing

public static void Update(List<InputData> lstAll)
    lstAll.resultData = lstAll.initalData + lstAll.formula;


As you can see from the code, each thread uses a bunch of objects. And I am updating only the resultData property of each object.

Is locking needed in this program? Can anyone please help

.net version is 3.5

  • 1
    Have you looked at the Thread-Safe Collections article on MSDN? In particular the System.Collections.Concurrent set of classes? ... though note, just because a collection is thread safe, it doesn't mean that the rest of the applications use of it is thread safe.
    – user40980
    Sep 28, 2014 at 14:52
  • So you mean to say that, i have to use locking before update. right?
    – khcha22
    Sep 28, 2014 at 14:58
  • Suppose, if i use array instead of collection. In that case, what should be done?
    – khcha22
    Sep 28, 2014 at 14:59
  • Realizing that this article is Java, the patterns described are still useful ones elsewhere. Java theory and practice: Thread pools and work queues.
    – user40980
    Sep 28, 2014 at 16:11
  • Why would you want to avoid locking? Are the couple of nanoseconds you will save worth the risk? Is it worth your time in analyzing the program for every change you make to ensure safety? Or are you better off writing the locks (or even better, using Tasks from .NET 4.0) and focusing your optimization on the processing?
    – Andrew
    Sep 28, 2014 at 19:56

2 Answers 2


You will need to analyze it like peeling an onion.

Changing the properties of objects that are stored within a List<> will not cause its index position to shift. It will not cause the List<> to perform any reallocations either.

Therefore, If the List<> are not being modified (objects inserted, deleted, moved, or replaced), then:

(1) you are right in that using List<> that is fully pre-initialized with objects would be sufficient.

But (2) you should then look at the next layer: does the computation of each object have any possibility of interfering with each other. Since we cannot see the code for the computation, we can't comment on that.

This analysis is only applicable to the List<> implementation provided in the .NET framework. If someone implements its own IList<>, with non-trivial logic and behaviors (such that they do not mimic the behaviors of the standard implementation at all, say, by autonomously modifying the ordering or index positions of objects) then the analysis does not apply.

  • Thanks for your answer. I will do this analysis. Any idea about array instead of list...
    – khcha22
    Sep 28, 2014 at 15:33
  • Also, as i have done pre-initialization, and you can see from the code there is no interfering too. So surely, locking is not needed.
    – khcha22
    Sep 28, 2014 at 15:38
  • 2
    As @DocBrown points out below, it appears that in the code that you did not show, you might be adding items to the List of results. If this is true, then depending on the details it might not be thread-safe. (In other words, look at all objects that are shared, and see whether they are modified and whether the modification will be unsafe.)
    – rwong
    Sep 28, 2014 at 19:15

As long as all of your five lists above contain disjoint sets of objects, and as long as the line


is commented out, there is no locking needed. But I guess you are telling us not the whole story, the program above in the current form does not make much sense, since any results are thrown away after processing. So tell us how and in which thread you intend to aggregate the results, then you might get better answers.

  • That commented line also will give the same result without locking. That leads to the question that whether locking is needed or not needed.
    – khcha22
    Sep 29, 2014 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.