Wednesday, 6 April 2016

c# thread workout

/*create 10 tasks with their execution time. Suppose processor can only handle 3 tasks simultaneously, and each task is finished without interference from other tasks.*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Collections;

namespace thread
{
    class Program
    {
        public static int number_of_job = 10;
        public static int active_job = 0;
        public static SortedList job_list = new SortedList();
        public static Object obj = new Object();

        static void Main(string[] args)
        {      
            job y = new job();
            Thread[] process = new Thread[10];

            for (int i = 0; i < 10; i++)
            {
                job_time_maker(ref job_list, i);

                process[i] = new Thread(new ThreadStart(y.do_job));
                process[i].Name = i.ToString();
                process[i].Start();

                Console.WriteLine("created thread # = " + i + " with run time = " + job_list[i]);
            }

            Console.WriteLine("**********done create threads");
            Console.Read();
        }

        static void job_time_maker(ref SortedList list, int key)
        {
            Random r = new Random();
            list[key] = r.Next(500, 3000);
        }

    }

    class job
    {
        public void do_job()
        {
            control call_control = new control();

            while (Program.number_of_job>0)
            {
                bool run = call_control.schedule('R');

                if (run == true)
                {
                    string current_thread_name = Thread.CurrentThread.Name;

                    Console.WriteLine("thread " + current_thread_name + " in the queue");

                    //key = thread[i].name            
                    int key = Convert.ToInt32(current_thread_name);

                    //sleep time = sortedlist[key ]
                    int sleep_time = Convert.ToInt32(Program.job_list[key]);
                    Thread.Sleep(sleep_time);
                    Console.WriteLine("thread " + current_thread_name + " completed in " + sleep_time + "ms");
                    call_control.schedule('C');
                    Thread.CurrentThread.Abort();
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
        }
     
    }

    class control
    {    

        public bool schedule(char a)
        {
            lock (Program.obj)
            {
                if (a == 'C')
                {
                    Program.number_of_job--;
                    Program.active_job--;
                    Console.WriteLine("active job number = " + Program.active_job + " remaining job = " + Program.number_of_job);
                    return true;
                }
                else
                {
                    if (Program.active_job >= 3)
                    {
                        return false;
                    }
                    else
                    {
                        Program.active_job++;
                        Console.WriteLine("active job number = " + Program.active_job);
                        return true;
                    }
                }
            }
        }
    }
}

------------------------------------------------------------------------------------------------

created thread # = 0 with run time = 2905
created thread # = 1 with run time = 2905
active job number = 1
thread 0 in the queue
active job number = 2
thread 1 in the queue
created thread # = 2 with run time = 2905
active job number = 3
thread 2 in the queue
created thread # = 3 with run time = 893
created thread # = 4 with run time = 893
created thread # = 5 with run time = 893
created thread # = 6 with run time = 1790
created thread # = 7 with run time = 2687
created thread # = 8 with run time = 2687
created thread # = 9 with run time = 2687
**********done create threads
thread 0 completed in 2905ms
active job number = 2 remaining job = 9
thread 1 completed in 2905ms
active job number = 1 remaining job = 8
thread 2 completed in 2905ms
active job number = 0 remaining job = 7
active job number = 1
thread 3 in the queue
active job number = 2
thread 5 in the queue
active job number = 3
thread 4 in the queue
thread 3 completed in 893ms
active job number = 2 remaining job = 6
thread 5 completed in 893ms
active job number = 1 remaining job = 5
thread 4 completed in 893ms
active job number = 0 remaining job = 4
active job number = 1
thread 6 in the queue
active job number = 2
thread 7 in the queue
active job number = 3
thread 9 in the queue
thread 6 completed in 1790ms
active job number = 2 remaining job = 3
active job number = 3
thread 8 in the queue
thread 7 completed in 2687ms
active job number = 2 remaining job = 2
thread 9 completed in 2687ms
active job number = 1 remaining job = 1
thread 8 completed in 2687ms
active job number = 0 remaining job = 0

No comments:

Post a Comment