What is multiple threads?

MULTITHREADING in Java is a process of executing two or more threads simultaneously to maximum utilization of CPU. Multithreaded applications execute two or more threads run concurrently. Hence, it is also known as Concurrency in Java. Each thread runs parallel to each other. Mulitple threads don't allocate separate memory area, hence they save memory. Also, context switching between threads takes less time.

Example of Multi thread:


  1. package demotest;
  2. public class pushpaThread1 implements Runnable
  3. {
  4. public static void main(String[] args) {
  5. Thread pushpaThread1 = new Thread("pushpa1");
  6. Thread pushpathread2 = new Thread("pushpa2");
  7. PushpaThread1.start();
  8. pushpathread2.start();
  9. System.out.println("Thread names are following:");
  10. System.out.println(pushpa Thread1.getName());
  11. System.out.println(pushpathread2.getName());
  12. }
  13. @Override
  14. public void run() {
  15. }
  16. }

Advantages of multithread:

  • The users are not blocked because threads are independent, and we can perform multiple operations at times
  • As such the threads are independent, the other threads won't get affected if one thread meets an exception.
Posted on by