How to do effective multithreading
-
hi, i'm new to multithreading and i found it abit hard to understand. For example, i i were to have 3 thread which each prints the letter 'A','B' and 'C' respectively for 3 times(meaning, in total there should be 3 As,3 Bs and 3 Cs) and it is suppose to output as such ABCAABBCC. But how can i do it? I thought of using Join method to ensure that the thread which writes A will execute the final 2 As and the same for Bs and Cs.....However, it does not seem to work... what should i take note of to ensure that it works accordingly? Below are the codes that i've written wrongly i suppose.
using System; using System.Threading; namespace MultiThreading2 { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Thread t1=new Thread(new ThreadStart(WriteA)); Thread t2=new Thread(new ThreadStart(WriteB)); Thread t3=new Thread(new ThreadStart(WriteC)); t1.Start(); t2.Start(); t3.Start(); t1.Join(); t2.Join(); t3.Join(); Console.ReadLine(); } static void WriteA() { for(int i=0;i<3;i++) {Console.WriteLine("A"); Thread.Sleep(2000); } } static void WriteB() {for(int j=0;j<3;j++) { Console.WriteLine("B"); Thread.Sleep(2000); } } static void WriteC() {for(int k=0;k<3;k++) { Console.WriteLine("C"); Thread.Sleep(2000); } } } }
Thanks for any replies:) -
hi, i'm new to multithreading and i found it abit hard to understand. For example, i i were to have 3 thread which each prints the letter 'A','B' and 'C' respectively for 3 times(meaning, in total there should be 3 As,3 Bs and 3 Cs) and it is suppose to output as such ABCAABBCC. But how can i do it? I thought of using Join method to ensure that the thread which writes A will execute the final 2 As and the same for Bs and Cs.....However, it does not seem to work... what should i take note of to ensure that it works accordingly? Below are the codes that i've written wrongly i suppose.
using System; using System.Threading; namespace MultiThreading2 { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Thread t1=new Thread(new ThreadStart(WriteA)); Thread t2=new Thread(new ThreadStart(WriteB)); Thread t3=new Thread(new ThreadStart(WriteC)); t1.Start(); t2.Start(); t3.Start(); t1.Join(); t2.Join(); t3.Join(); Console.ReadLine(); } static void WriteA() { for(int i=0;i<3;i++) {Console.WriteLine("A"); Thread.Sleep(2000); } } static void WriteB() {for(int j=0;j<3;j++) { Console.WriteLine("B"); Thread.Sleep(2000); } } static void WriteC() {for(int k=0;k<3;k++) { Console.WriteLine("C"); Thread.Sleep(2000); } } } }
Thanks for any replies:)Hi, the fundamental idea of multithreading is to perform multiple tasks in a rather asynchronous fashion, i.e. without predetermined order of execution; the goal is to make optimal use of available resources e.g. performing calculations on data as soon as such data is available. If the subjobs must be executed in a very specific order, multithreading simply makes no sense, since now the logic makes everything fully predictable. Examples of unpredictable execution could include: - some long winding calculations; - some user interaction; - a background printjob; - a database query; all of the above combined in one app. It would not make much sense to orchestrate this in a fixed manner since one never knows which subjob will take what time to execute, hence asyncrhonous execution, using several threads. In a typical application, there may well be a need to synchronize two or more threads from time to time. Several mechanisms are available for this, typically one thread waits for something (say a consumer needing some data), and another thread signals something (say a producer providing some data). Known mechanisms include: AutoResetEvent, ManualResetEvent, Queues, Semaphores, ... Joining two threads is a drastic synchronization since now one of the threads no longer runs. I would suggest you: - search and read some articles on multi-threading, here on CodeProject; - experiment with a more appropriate example. Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google