Blog

  • Multitasking In Java

    Posted by Nanda Ram | Last Updated: 06-Feb-18

    Multitasking

    Executing several tasks simultaneously is called multi-tasking.
    There are 2 types of multi-tasking
    1. Process-based multitasking
    2. Thread-based multi-tasking

     

    1. Process-based multi-tasking
    Executing various jobs together where each job is a separate independent operation, is called process-based multi-tasking.


    Example:                                                                                                                                                                                                                 

    While typing a java program in editor we can able to listen to audio songs by an mp3 player in our system at the same time we  can download a file from the net. All these jobs are executing together and independent of each other hence, this is  Process-based multi-tasking. Process-based multitasking is best suited at OS Level.

     

    2. Thread-based multi-tasking
    Executing several tasks simultaneously where each task is a separate independent part of the same program is called
    Thread-based multitasking and each independent part is called Thread. It is best suitable for the programmatic level. The main goal of multi-tasking is to make or do a better performance of the system by reducing response time. The main important application areas of multi-threading are developing video games, multimedia graphics, implementing animation etc. Java provides inbuilt support for multithreading by introducing a rich API (thread, runnable, thread group, thread local ) being a programmer we have to know how to use this API and we were not responsible to define that API. hence, developing multithreading programs is very easy in java when compared with C++.

     

    The ways to define, Instantiate and start a new Thread


    we can define a thread in the following 2 ways:
    1. By extending thread class
    2. By implementing runnable interface

     
    1. Define a thread by extending Thread class
    Example
     class Mythread extends Thread
    			 public void run()
    			 {
    			 for(int i=0; i <= 10; i++)
    			 {
    			 System.out.println("This is Child Thread");
    			}
    		}
    	}
    			  
    			 class SimpleThread
    			 {
    			 public static void main(String args[])
    			 {
    			 Mythread obj1 = new Mythread();
    			 obj1.start();       // Starting of a thread
    			 for(int i=0; i<= 10;i++)
    			 {
    			 System.out.println("This is Main Thread");
    			}
    		}
       } 
    
     
    2. Defining a thread by implementing Runnable Interface
    Example
     class MyRunnable implements Runnable
    			 public void run()
    			 {
    			 for(int i=0; i <= 5; i++)
    			 {
    			 System.out.println("Child Thread");
    			}
    		}
    	}
    			
    			 class SimpleRunnableDemo
    			 {
    			 public static void main(String args[])
    			 {
    			 MyRunnable obj = new MyRunnable();
    			 Thread t = new Thread(obj);
    			 t.start();       
    			 for(int i=0; i<= 10;i++)
    			 {
    			 System.out.println("This is Main Thread");
    			}
    		}
       }
    

Tags:

Mobile Applications

Video Content

Bigdata & NoSQL

SaaS Applications

Miscellaneous

Archives


Alexa Certified Site Stats for www.oodlestechnologies.com