0
2.9kviews
With the help of suitable example explain multithreading in terms of following:

(i) Creating threads, extending the thread class. - (ii) Life-cycle of thread. -

Marks: 10 M

Year: Dec 2013 , May 2014

1 Answer
0
41views
  • The thread is the smallest unit of dispatchable code. This means that a single program can perform two or more tasks simultaneously.
  • A broswer opened with two tabs is an example of multithreading.
  • We can create a thread by instantiating an object of type Thread. Java defines two ways in which this can be accomplished:
    • You can implement the Runnable interface.
    • You can extend the Thread class, itself.

Extending Thread Class:

  • In this , one can create a new class that extends Thread, and then to create an instance of that class.
  • The extending class must override the run( ) method, which is the entry point for the new thread.
  • It must also call start( ) to begin execution of the new thread. Here is the preceding program rewritten to extend Thread:

    class NewThread extends Thread 
    {
    NewThread()
    {
    // Create a new, second thread
    super("Demo Thread");
    System.out.println("Child thread: " + this);
    start(); // Start the thread
    }
    // This is the entry point for the second thread.
    public void run() 
    {
    try
    {
    for(int i = 5; i > 0; i--) 
    {
        System.out.println("Child Thread: " + i);
        Thread.sleep(500);
    }
    } 
    catch (InterruptedException e) { 
    System.out.println("Child interrupted"); 
    }
    System.out.println("Exiting child thread.");
    }//end of run()
    } //end of class
    class ExtendThread 
    {
    public static void main(String args[]) 
    {
    new NewThread(); // create a new thread
    try 
    {
        for(int i = 5; i > 0; i--) 
        {
            System.out.println("Main Thread: " + i);
            Thread.sleep(1000);
        }
        } catch (InterruptedException e) { 
        System.out.println("Main thread interrupted");
        }
           System.out.println("Main thread exiting");
           }//end of main
     } // end of ExtendThread class
    

Life-cycle of thread -

  • There are many states in which a thread can enter during its lifetime. These are:
  1. Newborn state
  2. Runnable state
  3. Running state
  4. Blocked state
  5. Dead state
  6. A thread is always in one of these five states. It can be shifted from one state to another via variety of ways as shown in figure.

enter image description here

1) Newborn state

  • After creation of the thread object, the thread is born which is called as newborn thread.
  • This thread is not scheduled for running. We can either start this state to make it runnable using start( ) method or kill the thread using stop( ) method. Only these two operations can be performed on this state of the thread.
  • If we attempt to perform any other operation, the exception will be thrown.

2) Runnable state

  • When the thread is ready for execution and is waiting for availability of the processor, it is said to be in runnable state.
  • The thread has joined the queue of threads that are waiting for execution. If all the threads have equal priority, then they are given time slots for execution in round robin fashion i.e. on first come first serve basis.
  • The thread that relinquishes control joins the queue at the end and again waits for its execution.
  • This process of assigning time to threads is known as time slicing. If we want a thread to relinquish control to another thread of equal priority, a yield ( ) method can be used.

3) Running state

  • When the processor has given time to the thread for its execution then the thread is said to be in running state. The thread runs until it relinquishes control to its own or it is preempted by a higher priority thread. A running thread may relinquish its control in any one of the following situations:
  • The thread has been suspended using suspend( ) method. This can be revived by using resume( ) method. This is useful when we want to suspend a thread for some time rather than killing it.
  • We can put a thread to sleep using sleep (time) method where ‘time’ is the time value given in milliseconds. Means, the thread is out of queue during this time period.
  • A thread can wait until some event occurs using wait( ) method.
  • This thread can be scheduled to run again using notify( ) method.

4) Blocked state

  • When a thread is prevented from entering into the runnable state and subsequently in running state it is said to be blocked.
  • This happens when the thread is suspended, sleeping or waiting in order to satisfy certain requirements.
  • A blocked thread is considered “not runnable” but not dead and therefore fully qualified to run again.

5) Dead state

  • A running thread ends its life when it has completed its execution of run( ) method.
  • It is a natural death. However we can kill it by sending the stop message to it at any state thus causing a premature death to it.
  • A thread can be killed as soon as it is born or while it is running or even when it is in blocked state.
Please log in to add an answer.