This is a very simple program to demonstrate the use of Thread Join . Please follow my comments for a better understanding. Record this program as is.
using System; using System.Threading; namespace ThreadSample { class Program { static Thread thread1, thread2; static int sum=0; static void Main(string[] args) { start(); Console.ReadKey(); } private static void Sample() { sum = sum + 1; } private static void Sample2() { sum = sum + 10; } private static void start() { thread1 = new Thread(new ThreadStart(Sample)); thread2 = new Thread(new ThreadStart(Sample2)); thread1.Start(); thread2.Start();
1. The first time starts as is (with comments): Then the result will be 0 (the initial value) or 1 (at the end of stream 1) or 10 (or the end of the stream)
2.Run with deleting the thread1.Join() comment: The result should always be more than thread1.Join() fired, and thread 1 must be finished before receiving the amount.
3.Run with deleting all comments: The result should always be 11
Elshan Oct 23 '14 at 19:39 2014-10-23 19:39
source share