Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, i wanna ask you the difference about this one :

C#
// 1
oThread1 = new (Method1);
oThread2 = new (Method2);
oThread3 = new (Method3);

oThread1.Start(obj);
oThread2.Start(obj);
oThread3.Start(obj);


C#
// 2
oThread1 = new (Method1);
oThread2 = new (Method2);
oThread3 = new (Method3);

oThread1.Start(obj);
oThread2.Start(obj);
oThread3.Start(obj);

oThread1.Join();
oThread2.Join();
oThread3.Join();


And would u tell me the mechanism of each other (process), so it can show me the difference... :~
Thank you.
Posted

If the Main thread spawns a child thread, the child thread is killed as soon as the main thread is killed. So, if your child thread requires 10 seconds to execute, and, if your main thread completes execution within 5 seconds, the main thread will be killed and your child thread will be killed immediately no matter whether it is completed or not. You obviously don't want that to happen.

So, you would like your main thread to wait until your child thread completes execution.

If you call childThread.Join(), your main thread waits until the childThread completes it's execution.

So, I think you already understood the difference between these two options you provided :)
 
Share this answer
 
Comments
Teamsar Muliadi 31-Aug-10 3:15am    
Mmmm... i see.. :), but.. according to you.. which one is the best practice from these cases above ??
Al-Farooque Shubho 31-Aug-10 4:15am    
Well, between these two, obviously the 2nd one.
Not sure that syntax is correct, but the difference is the three Join methods called on the threads in the second code block. A Join will wait for that thread to complete processing before allowing the current thread to proceed. So, it will wait for thread 1, then 2, then 3 to finish. Though all three threads may be running at once, so once the current thread finishes waiting for thread 1 to complete, it may just pass straight through the second and third threads (because they may already be done).
 
Share this answer
 
Comments
Teamsar Muliadi 31-Aug-10 3:12am    
Mmm.. but, how about in the first case ??
AspDotNetDev 31-Aug-10 3:20am    
The current thread will not wait for the other three threads to execute. In essence, 4 threads will be running at once, with each of them ending at some point or another (not necessarily at the same time). That means that whatever processing that occurs after oThread3.Start(obj); will execute at the same time the code in threads 1-3 execute. Depending on if those 3 threads are background threads, they may exit prematurely if the current thread executes before they finish (supposing the current thread is the main application thread).
Teamsar Muliadi 31-Aug-10 3:27am    
Ok.. :) thank you aspdotnetdev.. :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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