Threading in java

Introduction 

 
In this blog, we will know about threading in java.
 
Thread: - A single flow of control in a program is known as thread. This can execute independently in a processing environment and perform an operation. A program can be divided into different parts or different sequential flow of control known as a multithreaded program.
 

How to create a thread

 
A thread can be created by a java program by using
 
1.Java.lang.thread class
2.java.lang.Runnable interface

 
Creating of thread using thread class

 
1.create a class by inheriting thread class is required to provide a constructor to the class.
2.Override run () method to provide the logic for thread, which can execute independently, run () method contains thread body.
3.Create an interface if the class which is created in step-1
4. To start the threads execution use start () method which calls the run () method.
 
Example
 
threadDemo.java file
  1. class mythread extends Thread {  
  2.  public void run() {  
  3.   for (int i = 0; i < 100; i++)  
  4.    System.out.println("Welcome to multithreading " + i);  
  5.  }  
  6. }  
  7. public class threadDemo {  
  8.  public static void main(String arg[]) {  
  9.   System.out.println("Main Started");  
  10.   mythread m = new mythread();  
  11.   m.start();  
  12.   System.out.println("main finished");  
  13.  }  
  14. }  
Compile
 
javac threadDemo.java
 
java threadDemo
 

Creating thread using runnable interface

 
1. Create a class, which must implement java.lang.Runnable interface.
2.Override run () method to provide a body of the thread.
3. Create an instance of thread class which must accept an instance of the class created in step-1 (subclass of runnable interface).
4. Start the execution of the thread call start method in the Thread class instance.
 
Example
 
thread_demo.java file
  1. import java.util.*;   
  2. class mythread implements Runnable  
  3. {  
  4.  public void run()    
  5.  {  
  6.   Scanner sc = new Scanner(System.in);  
  7.   System.out.println("Provide ur number");  
  8.   int x = sc.nextInt();  
  9.   System.out.println("No is " + x);  
  10.  }   
  11. }   
  12. public class thread_demo   
  13. {   
  14.  public static void main(String arg[])  
  15.  {  
  16.   System.out.println("Main Started");  
  17.   mythread m1 = new mythread();  
  18.   //to call run() of mythread class use its object in constructor of Thread class    
  19.   Thread th = new Thread(m1);  
  20.   th.start();  
  21.   try  
  22.   {  
  23.    //Thread.sleep(3000);    
  24.    /* join() suspends parent thread's execution till the child upon which its invoked is not finished.  
  25. isAlive() returns true if thread is not finished else false */  
  26.    if (th.isAlive())  
  27.     th.join();  
  28.   } catch (Exception e) {}  
  29.   System.out.println("Main finished");  
  30.  }   
  31. }  
Compile
 
javac thread_demo.java
 
java thread_demo
 

Difference between multiprocessing and multithreading

 
Multiprocessing
 
1. A program in execution is known as process. Multiple processes for a single environment are known as multiprocessing.
2. The minimum unit of multiprocessing is a program.
3. Process occupies its own resources or memory.
 
Multithreading
 
1. A part of a program, which can execute independently, is known as Multithreading.
2. The minimum unit of Multithreading is thread or part of a program.
3. Thread occupies the resources available to its process. This also is called a lightweight process.