First Step to Java's Multithreading

What is MultiThreading in Java?

 
Multithreading in Java is a process of executing multiple threads simultaneously. A thread is the smallest unit of the processing. Multithreading and Multiprocessing, both are used to achieve multitasking.
 

Advantages of Multithreading

  •     Optimize performance of an application.
  •     Reduce the computation time.
  •     Saves memory, because threads share the same memory space.
  •     Context switching between threads is less expensive.
  •     Provides a specialized form of multitasking.

Disadvantages of MultiThreading

  • Multiple threads can interfere with each other when sharing hardware resources such as caches or transaction lookaside buffers(TLBs).
  • The execution time of a single thread can be degraded, even when only one thread is executing.
  • Complex debugging and testing process.

What is Thread in Java?

 
A thread is a lightweight process (part of a program) or flow of control within a Java program. The JVM allows a program/application to have more than one thread and the threads can run concurrently; such a program is called a Multithreaded Program and the approach is known as Multithreading.
 
In Java's multithreading, the Thread Class & Runnable Interface are used to create and manage threads. Each and every Java program has a thread, i.e. the main thread. The main thread runs immediately when a Java program is started. All other threads are engendered from the main thread. The main thread must be the last thread to finish execution because it performs various shutdown actions, otherwise the system may hang.

The main thread is controlled using the currentThread() method; currentThread() is a public static member of the Thread class. The currentThread() method returns the reference to the thread in which it is called.

For example,
  1. public class JavaThreadingDemo   
  2. {  
  3.     public static void main(String[] args)   
  4.     {   
  5.         //Reference to the current thread is obtained and stored in t  
  6.         Thread t = Thread.currentThread();  
  7.   
  8.         //Displays the information of thread  
  9.         System.out.println("Current Thread is " + t);  
  10.   
  11.         //To change the name of the thread  
  12.         t.setName("MyThread");  
  13.   
  14.         //To change the priority of the thread  
  15.         t.setPriority(4);  
  16.   
  17.         //Again, displaying the information of thread  
  18.         System.out.println("Current Thread is " + t);  
  19.   
  20.         try {  
  21.             for (int i = 5; i > 0; i--) {  
  22.                 System.out.println("*");  
  23.   
  24.                 //Pause for one second  
  25.                 Thread.sleep(1000);  
  26.             }  
  27.             //Sleep() method may throw InterruptedException, so we are wrapping the  
  28.             //code in try-catch block  
  29.         } catch (InterruptedException e) {  
  30.             System.out.println(e);  
  31.         }  
  32.     }  

Output 
 

 java-threading-demo

Notice the first line of the output; it shows the information about the thread i.e., Thread[main,5,main], where main is the name of thread, 5 is the priority of the thread (default value) and main is thread group.
 

Create New Thread by Implementing Runnable Interface

  1. class NewThread implements Runnable {  
  2.     Thread t;  
  3.   
  4.     NewThread() {  
  5.         // Create a new thread, second thread  
  6.         t = new Thread(this);  
  7.         //"this" indicates that you need a new thread to call run() on this object  
  8.   
  9.         t.start(); // Start the thread, call run()  
  10.     }  
  11.   
  12.     public void run() {  
  13.         System.out.println("Inserting Child Thread.....");  
  14.         try {  
  15.             for (int i = 10; i > 0; i--) {  
  16.                 System.out.println("*");  
  17.                 Thread.sleep(500);  
  18.             }  
  19.         } catch (InterruptedException IEx) {  
  20.             System.out.println(IEx);  
  21.         }  
  22.         System.out.println("Exiting Child Thread.....");  
  23.     }  
  24. }  
  25.   
  26. public class JavaThreadRunnable {  
  27.     public static void main(String[] args) {  
  28.         System.out.println("Inserting Main Thread....");  
  29.         new NewThread();  
  30.         try {  
  31.             for (int i = 10; i > 0; i--) {  
  32.                 System.out.println("+");  
  33.                 Thread.sleep(1000);  
  34.             }  
  35.         } catch (InterruptedException IEx) {  
  36.             System.out.println(IEx);  
  37.         }  
  38.         System.out.println("Existing Main Thread.....");  
  39.     }  

Output

java-thread-runnable 
 

Summary

 
In this article, we learned about multiThreading in Java and creating the threads in Java progrms.
 
To read more about MultiThreadin in Java Click