Click here to Skip to main content
15,868,073 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a collection of objects for ex

C#
List<Employee> objEmployeeList = new List<Employee>();

            for (int i = 1; i < 300; i++)
            {

                Employee objEmployee = new Employee { FName = "test" + i, Name = "test" + i };
                objEmployeeList.Add(objEmployee);
            }


I have this collection of 300 objects...

i want to split this into 30 parts and send them to 30 Threads,

For ex i have a method
C#
Processeemployee(Employee objEmployee)


I want to create 30 threads for this method and each method should process 30 objects parallely.. Is this possible..


Please help me..
Posted
Updated 10-Feb-12 15:35pm
v3
Comments
Sergey Alexandrovich Kryukov 10-Feb-12 20:54pm    
Do you understand that in ASP.NET threads are nearly useless? The whole process lifetime goes from HTTP request to HTTP response, that's it...
--SA

Please see my comment about ASP.NET.

Now, let's analyze the effect of possible parallel execution of your loop. Construction of each instance of Employee is independent, so each can be created in parallel. But we can reasonably assume that a constructor execution is very fast, would hardly be slower than adding a new element to the list (if it is slower, it would mean you are doing something fundamentally wrong with this constructor).

But now, pay attention that objEmployeeList is a resource shared by all your threads. It means that is should be done thread-save. That said, you could use for objEmployeeList either the thread-safe type System.Collections.Concurrent.ConcurrentBag<Employee> or a "regular" list class System.Collections.Generic.List<Employee>, but the Add operation should be performed under the lock statement.

Please see:
http://msdn.microsoft.com/en-us/library/dd381779.aspx[^],
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^],
http://msdn.microsoft.com/en-us/library/c5kehkcz%28v=vs.80%29.aspx[^].

Is is good or bad? It will practically nullify the effect of parallel execution, because thread-safe adding the elements to the same collection will serialize all threads by putting them in a queue waiting for access to Add. Each thread will quickly execute the constructor and quickly take its place in the same single queue of the threads each waiting for the access to Add.

What's the practical effect of it? It will actually slow down the execution of this task, because you won't gain considerable parallelization even on many CPU cores; at the same time, the overhead cost of using threads and synchronization will eat up a good part of CPU time.

Instead of explicit use of thread you could better use System.Threading.Tasks.Parallel.ForEach, http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.aspx[^].

But will it really be much better in this case? No! The exact same considerations will be applicable, with the same result: parallel execution is useless in this case.

The code using parallel execution for performance gain is neither usual not trivial. This approach is not applicable to everything; and the set of reasonable tasks is limited, the application of parallel execution should start with thorough analysis and design. In many cases, parallel execution as such does not help to gain sheer performance.

Eastern proverb says:
"Just saying 'halva-halva' won't make your mouth sweet."


—SA
 
Share this answer
 
v7
Comments
Wonde Tadesse 10-Feb-12 22:52pm    
5+.
Sergey Alexandrovich Kryukov 11-Feb-12 1:28am    
Thank you very much, Wonde, even though you probably forgot to vote... :-)
--SA
Wonde Tadesse 11-Feb-12 10:54am    
Opps. How about now?
charan49 13-Feb-12 1:14am    
Hi Thanks for your response...
let me give my exact scenario.. we have a client weservice that is capable of processing 5 request concurently..

lets say now i have 50 request objects which are already in my list objectc.. I have to create 5 threads and pass 10 request objects to each thread.. So that we make use of webservice most effectiverly.. This has to be 5 request should go to webservice at any given point..
Espen Harlinn 11-Feb-12 5:08am    
5'ed!
Answer is yes.

Go for the back ground process worker. It's provide you thread safety.

 
Share this answer
 
v2
Comments
Chris Maunder 10-Feb-12 22:27pm    
No, it's not a lie. You can create background threads in ASP.NET and in some (rare) situations it's a very effective tool. We do it ourselves.
Sergey Alexandrovich Kryukov 11-Feb-12 0:27am    
Sorry, it is. A background worker is just a wrapper of a thread; and as such it does not provide thread safety. If you have a shared resource, you still cannot access it from different threads without some synchronization primitives anyway, no matter how a thread was created.

The term "thread-safe" is usually applied to a resource which can be accesses by different threads.

Having some features like "progress" implemented in thread-safety manner does not justify calling a whole class "thread-safe". At least without any explanations.

Remember also, this is said in the context of this question. The resource which needs thread safety if the shared list in the loop. The statement about "thread-safety" provokes the idea that if the iterations are done in different threads executed using BackgroundWorker provides thread safety, which is not true. The access to the list's Add should be done under lock (or a thread-safe collection should be used), which is in fact totally irrelevant to the way a thread was obtained.

Well, I agree this class can have its uses, I more or less agree with your "rare situations", but it does not make the statement of thread safety correct.

Now, about the first statement -- the recommendation of using BackgroundWorker in the case OP was interested in. In my answer, I explain why parallel execution will not improve performance and why parallel execution can do it even worse then sequential. The thing is: two thread-using options would make the situation especially bad: a thread obtained through a Thread.Thread constructor and background worker. Some better methods are: 1) using Parallel.ForEach, where threads are used by its dispatching tasks to threads is done automatically by the library; 2) thread pool; 3) re-use a set of pre-created threads kept is a wait state when there is not a task for them; of course this last option looks like a manual and impractical implementation of parallel for each and makes no sense in this situation, but it could be considered in principle (it is very good for many situations where there is a fixed number of threads each reserved for a fixed logical processing).

If you think that this answer has anything beyond just remembering "a good way" of using a thread and is based on any rational analysis of the problem offered by OP, I would suggest you thing again. In my opinion, this is nothing more then a "me too" answer, without any responsibility for the advice.

Imagine that I did not comment at all and allow our pretty naive OP (but a person who honestly asked quite a legitimate question as the problem is not so easy to analyze of a beginner), imagine I would allow OP to take this advice seriously. It would be possible in principle. Wasting so much time along this long time could potentially cause some harm to OP.

And "make no harm" is pretty important principle, I think. I somebody reasonable argues that I gave a wrong advise, I try to fix it as soon as possible and express my gratitude to the one who criticized me. Wrong advice can be really harmful, much worse then no advice at all...

Thank you.
--SA
Chris Maunder 11-Feb-12 14:15pm    
My apologies - I did not take note of the "it will provide thread safety". You are absolutely correct in that thread safety is not provided by default and will have to be accounted for.
charan49 14-Feb-12 7:17am    
Hi Thanks for your response... let me give my exact scenario.. we have a client weservice that is capable of processing 5 request concurently.. lets say now i have 50 request objects which are already in my list objectc.. I have to create 5 threads and pass 10 request objects to each thread.. So that we make use of webservice most effectiverly.. This has to be 5 request should go to webservice at any given point..
ksanghi 10-Feb-12 23:26pm    
Hi Chris,
Can u explain what you ant to express.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900