Thread: A sub-process or a smallest unit of processing, called thread. Generally a single thread work only on a single sub-process which may or may not be the part of whole processing to achieve any task. A thread is also a lightweight process.
Multithreading: To complete any single process or to complete any task, if multiple threads are working simultaneously, then it’s called multithreading. Technically, all the threads do not execute simultaneously, but the time difference between the threads execution is very less so that it seems simultaneously. There are multiple scheduling systems to define thread execution schedule for example Round Robin scheduling.
Real Time Example of Multithreading in Java
As Java supports multithreading concept, where multiple threads can handle different task at same time concurrently and makes optimal use of available resources, if computer has multiple CPUs.
Most of the beginner java developer feel multithreading concept complex and confusing. To understand this concept, we have some real time example where we implement multithreading.
Example 1: Most of us use cell phones to talk with friends and other peoples along with some other work. Some smart people eat the food and talk on the phone and some extra smart people also watch the television with these two activities.
Example 2: Banking system use multithreading concept, when someone withdraw the amount at same time, user get message on phone and get email as well. So that, we can say one thread is working to send message on phone and another thread in working to send email and there may be more threads, giving example only for two major threads.
Basically, multithreading is useful for those application where we need multiple tasks to be done parallelly. If any system has single processor in that case multiple threads share the CPU time which is knows as Time-Sharing.
From java perspective, there is a fixed lifecycle of any thread. Below given diagram shows the different stat of any thread in java.
How to Implement Multithreading in Java
We can achieve multithreading in java by implementing Runnable interface, there is also another way by extending Thread class but internally Thread class implements Runnable interface. One point to be noted here that Runnable is an interface while Thread is a class which is implementing Runnable interface.
1. Example with Single Thread:
public class Mobile { public static void main(String [] arg) { System.out.println(“This is single thread”); } }
Here, we are creating only single thread which is also an default thread.
2. Example with Multiple Threads:
public class Mobile implements Runnable { public static void main(String [] arg) { Thread t1 = new Thread(“Thread_1”); Thread t2 = new Thread(“Thread_2”); t1.start(); t2.start(); System.out.println(“Below are the thread names”); System.out.println(t1.getName()); System.out.println(t2.getName()); } @Override public void run(){ } }
Here, We are implementing Runnable interface and creating two different threads with name Thread_1 and Thread_2. After starting both of these threads we can print their names just by calling getName() method.
Advantages of Multithreading
- In any multithreaded application, different thread work on different parts of application.
- Multiple thread can share same memory space and also use thread specific memory.
- For multiprocessor system, different thread executed on different processor in parallel and make whole processing fast.
- Multithreading reduce the computation time.
- It maximize the CPU utilization by using multiple thread at same time.
Disadvantages of Multithreading
- Multithreading makes whole process complex.
- Shared resources should be synchronized.
- Increase the chance of deadlock.
- Programing complexity may occur.
Conclusion
Multithreading happens within the same process by breaking it into multiple sub-processes and by executing all these sub-processes on multiple threads. This is the core concept of multithreading.