Recent Posts

Wednesday, 3 August 2016

Threads And Concurrency Tutorial


Multitasking
   Executing several tasks simultaneously is called ‘Multitasking’, There are 2 types of multitasking.
1. Process based Multitasking
     Executing several tasks simultaneously where each task is a separate independent process is called 'Process based Multitasking'.
E.g
     While writing java program in the editor we can run MP3 player. At the same time we can download a file from the net. All these tasks are executing simultaneously and independent of each other. Hence it is process based Multitasking. Process based Multitasking is best suitable at O.S level.

2. Thread based Multitasking
     Executing several tasks simultaneously where each task is a separate independent part of the same program is called 'Thread based Multitasking'. This type of multitasking is best suitable at programmatic level.
   Java provides in built support for thread based multitasking by providing rich library (Thread, ThreadGroup, Runnable ..etc)

     Whether it is Processbased or Threadbased the main objective of multitasking is to reduce response time and improve performance of the system. The main application area of multithreading is videogames implementation, Multimedia graphics , to develop animations etc.

Defining Instantiating, Starting the Thread
     We can define instantiate and starting a thread by using the following 2 ways.

1. By extending Thread Class
E.g
class MyThread extends Thread {
   public void run() {
      for (int i = 0; i < 5; i++) {
         System.out.println("Child Thread");
      }
   }
}

public class MultiThreadDemo {
   public static void main(String[] args) throws InterruptedException {
      MyThread t = new MyThread();
      t.start();
      for (int i = 0; i < 5; i++) {
         System.out.println("Main Thread");
      }
   }
}
Case1
Thread Shedular
     If multiple threads are there then which thread will get chance first for execution will be decided by “Thread Scheduler”. Thread Scheduler is the part of JVM. The behavior of thread scheduler is vendor dependent and hence we can’t expect exact O/P for the above program.
The possible Outputs are
Main Thread       Child Thread      Main Thread 
Main Thread       Child Thread      Child Thread
Main Thread       Child Thread      Main Thread 
Main Thread       Child Thread      Child Thread 
Main Thread       Child Thread      Main Thread 
Child Thread      Main Thread       Child Thread 
Child Thread      Main Thread       Main Thread 
Child Thread      Main Thread       Child Thread 
Child Thread      Main Thread       Main Thread 
Child Thread      Main Thread       Child Thread
Case 2
Difference between t.start() and t.run() methods.
* In the case of t.start() a new Thread will be created which is responsible for the execution of run() method.

* But in the case of t.run() no new Thread will be created and run() method will be executed just like a normal method by the main Thread.

* In the above program if we are replacing t.start() with t.run() the following is the output.
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Case 3
Importance of Thread Class start() method
     After Creating thread object compulsory we should perform registration with the thread scheduler. This will take care by start() of Thread class, So that the programmers has to concentrate on only job. With out executing Thread class start() method there is no chance of start a new Thread in java.
start() {
   Register our thread with the thread scheduler.
   Invoke run method.
}
Case 4
If we are not overriding run() method
     If we are not overriding run() method then Thread class run() method will be executed which has empty implementation and hence we won't get any output.
E.g
class MyThread extends Thread {

}
class ThreadDemo {
   public static void main(String[] args)  {
      MyThread t=new MyThread();
      t.start();
   }
}

Output
No output
    It is highly recommended to override run() method. Otherwise don't go for multithreading concept.

Case 5
Overloding of run() method.
     We can overload run() method but Thread class start() method always invokes no argument run() method the other overload run() methods we have to call explicitly then only it will be executed just like normal method.
E.g
class MyThread extends Thread {
   public void run() {
      System.out.println("Inside no arg method");
   }
   public void run(int i) {
      System.out.println("Inside int arg method");
   }
}
class ThreadDemo {
   public static void main(String[] args) {
      MyThread t=new MyThread();
      t.start();
   }
}
Output:
Inside no arg method
Case 6
Overriding of start() method
If we override start() method then our start() method will be executed just like a normal method call and no new Thread will be started.
E.g
class MyThread extends Thread {
   public void start() {
      System.out.println("Inside start method");
   }
   public void run() {
      System.out.println("Inside run method");
   }
}
class ThreadDemo {
   public static void main(String[] args) {
      MyThread t=new MyThread();
      t.start();
      System.out.println("Inside main method");
   }
}
Output:
Inside start method
Inside main method
Case 8
Life cycle of the Thread




* Once we created a Thread object then the Thread is said to be in new state or born state.
* Once we call start() method then the Thread will be entered into Ready or Runnable state.
* If Thread Scheduler allocates CPU then the Thread will be entered into running state.
* Once run() method completes then the Thread will entered into dead state.

Case 9
     After starting a Thread we are not allowed to restart the same Thread once again otherwise we will get runtime exception saying "IllegalThreadStateException".
E.g
class MyThread extends Thread {
   public void run() {
      System.out.println("Hey this is run method");
   }
}
class ThreadDemo {
   public static void main(String[] args)  {
      MyThread t=new MyThread();
      t.start();
      System.out.println("Hi main method"); // Fine
      t.start();
      System.out.println("Bye main method"); //we will get R.E saying: IllegalThreadStateException
   }
}

Next Tutorial  Threads and Concurrency Tutorial Part 2

Previous Tutorial  Generics Tutorial

No comments:

Post a Comment