Home > Development > Basic Thread Coordination Part 1

Basic Thread Coordination Part 1


When threads are created they run interdependently from the thread that created them. This is exactly what we want. However we often need to wait for multiple threads doing work to finish before continuing. This is done with the Join() method on the thread which blocks the main thread while waiting till the thread completes.

using System;
using System.Threading;

namespace ThreadsAreForScheduling
{

    public static class ThreadExtensions
    {
        public static void JoinAll(params Thread[] threads)
        {
            Array.ForEach(threads, thread => thread.Join());
        }

        public static void JoinAll(TimeSpan timeout, params Thread[] threads)
        {
            Array.ForEach(threads, thread => thread.Join(timeout));
        }
    }

    public class Program
    {
        public static void Main()
        {

            Program p = new Program();
            p.CreatingThreadsToToRunCalculations();

            Console.WriteLine("Press any key to quit...");
            Console.ReadKey();
        }

        public void CreatingThreadsToToRunCalculations()
        {
            Thread t1 = new Thread(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("10^1: {0}", CalculatePower(10, 1));
            });
            Thread t2 = new Thread(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("10^2: {0}", CalculatePower(10, 2));
            });
            Thread t3 = new Thread(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("10^3: {0}", CalculatePower(10, 3));
            });
            Thread t4 = new Thread(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("10^4: {0}", CalculatePower(10, 4));
            });

            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start();

            ThreadExtensions.JoinAll(t1, t2, t3, t4);
            
            // The above is equivalent to
            // t1.Join();
            // t2.Join();
            // t3.Join();
            // t4.Join();

        }

        private int CalculatePower(int a, int power)
        {
            if (power == 1) return 10;
            int value = 10;

            for (var i = 1; i < power; i++)
            {
                value *= 10;
            }
            return value;
        }
    }
}

Blair

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: