What is Multi-Threading and how we can define a thread?

What is Multi-Threading and How many ways we can define a thread.

What is Multi-Threading and how we can define a thread?

What is Multi-Threading and how we can define a thread?
  • 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 a thread.
  • Thread-based multitasking is best suitable at a programmatic level.
  • Whether it is process-based or thread-base the main objective of multitasking is to reduce the response time of the system and to improve performance.
  • There are two types of threads in an application – user thread and daemon thread. When we start an application, main is the first user thread created and we can create multiple user threads as well as daemon threads. When all the user threads are executed, JVM terminates the program.
  • The main important application are areas of multithreading are
    • To develop multimedia graphics.
    • To develop animations.
    • To develop video games.
    • To develop web-servers and application servers.
  • When compared with old languages developing multithreaded applications in Java is very easy. Because Java provides inbuilt support for multithreading with rich API(Thread, Runnable, Thread group…).
  • The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread.
  • Threads are lightweight sub-processes, they share the common memory space. In a Multithreaded environment, programs that are benefited from multithreading, utilize the maximum CPU time so that the idle time can be kept to the minimum.
  • We can define a thread in the following two ways…

By extending Thread class.

package com.java4us
class Test extends Thread{  
  public void run(){  
    System.out.println("My thread is in running state.");  
  }   
  public static void main(String args[]){  
     Test test=new Test();   
     test.start();  
  }  
}

By implementing Runnable Interface

package com.java4us
class Test implements Runnable{  
  public void run(){  
    System.out.println("My thread is in running state.");  
  }   
  public static void main(String args[]){  
     Test test=new Test();  
     Thread t =new Thread(Test);  
     t.start();  
 }  
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*