just want some advice on "best practice" regarding multi-threading tasks.
as an example, we have a C# application that upon startup reads data from various "type" table in our database and stores the information in a collection which we pass around the application. this prevents us from hitting the database each time this information is required.
at the moment the application is reading data from 10 tables synchronously. i would really like to have the application read from each table in a different thread all running in parallel. the application would wait for all the threads to complete before continuing with the startup of the application.
i have looked into BackGroundWorker but just want some advice on accomplishing the above.
i look forward to some answers
Barriers. Barriers are a synchronization mechanism that can be used to coordinate multiple threads working in parallel. A barrier allows each thread to wait until all cooperating threads have reached the same point, and then continue executing from there.
If you create thousands of threads then you will waste time context switching between them and your work will take longer to complete. Instead of manually starting new threads you should use the thread pool to perform your work so Windows itself can balance the optimum number of threads.
Behind the scenes it is using default JVM's fork join pool which means that it will wait for all the threads to finish before continuing.
The wait() is used in with notify() and notifyAll() methods, but join() is used in Java to wait until one thread finishes its execution. wait() is mainly used for shared resources, a thread notifies other waiting thread when a resource becomes free. On the other hand join() is used for waiting a thread to die.
My preference for this is to handle this via a single WaitHandle, and use Interlocked to avoid locking on a counter:
class Program { static void Main(string[] args) { int numThreads = 10; ManualResetEvent resetEvent = new ManualResetEvent(false); int toProcess = numThreads; // Start workers. for (int i = 0; i < numThreads; i++) { new Thread(delegate() { Console.WriteLine(Thread.CurrentThread.ManagedThreadId); // If we're the last thread, signal if (Interlocked.Decrement(ref toProcess) == 0) resetEvent.Set(); }).Start(); } // Wait for workers. resetEvent.WaitOne(); Console.WriteLine("Finished."); } }
This works well, and scales to any number of threads processing, without introducing locking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With