Breaking News

Thursday 24 October 2013

Thread State

Thread State :

every object has its state. For example : water is found in three states , such as solid , liquid , and gas and it can be transform from one state to another. In the same way , threads also have their states and can be transformed from one state to another. The state of thread determines its actual status at a given point of time. you can determine state of a thread by the ThreadState property of the Thread Class.


States of threads:


Name
Description
Running
Refers to a state in which the thread is in the execution mode.
StopRequested
Refers to a state in which a thread is requested to stop its execution.
SuspendRequested
Refers to a state in which a thread is required to suspend its execution.
Background
Refers to a state in which a thread is execute as a background thread , as opposed to foreground thread.
Unstarted
Refers to a state in which  the start() method of the thread class has not yet been invoked.
Stopped
Refers to a state in which a thread has already stopped executing.
WaitSleepJoin
Refers to a state that occurs when the wait() , sleep() , and join() method are simultaneously invoked. A thread in this state stops executing and is considered  to be blocked.
Suspended
Refers to a state in which a thread has been suspended.
AbortRequsted
Refers to state in which a thread is required to stop its functioning permanently.
Abort
Refers to a state in which a thread is considered to be dead forever.


showing the code of the Thread State application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace thread_state.cs
{
    class Program
    {
        static Thread firstthread;
        static Thread secondthread;
        static Thread thirdthread;
        static void Main(string[] args)
        {
            firstthread = new Thread(firstfunction);
            secondthread = new Thread(secondfunction);
            thirdthread = new Thread(thirdfunction);
            firstthread.Name = "first thread";
            secondthread.Name = "second thread";
            thirdthread.Name = "third thread";
            StateOfThread("main() before starting the threads..");
            firstthread.Start();
            secondthread.Start();
            thirdthread.Start();
            StateOfThread("main() before ending the threads..");
        }
        // writting a function for determining the state of the thread
        public static void StateOfThread(string position)
        {
            Console.WriteLine("\n in" + position);
            Console.WriteLine("thread name :\t" + firstthread.Name + "thread state\t" + firstthread.ThreadState);
            Console.WriteLine("thread name :\t" + secondthread.Name + "thread state\t" + secondthread.ThreadState);
            Console.WriteLine("thread name :\t" + thirdthread.Name + "thread state\t" + thirdthread.ThreadState);
        }
        public static void firstfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("first thread displays\t" + num);
                Thread.Sleep(2000);
            }
            StateOfThread("firstfunction()");
        }
        public static void secondfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("second thread displays\t" + num);
                Thread.Sleep(5000);
            }
            StateOfThread("secondfunction()\t");
        }
        public static void thirdfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("third thread displays\t" + num);
                Thread.Sleep(7000);
            } 
            StateOfThread("thirdfunction()");
            Console.ReadKey();
        }
    }

}

output :




No comments:

Post a Comment

© 2013 Post by repter_x