Multithreading in Java allows executing multiple threads simultaneously by using lightweight threads that share a common memory area. This saves memory compared to multiprocessing and has lower context switching costs. The main advantage of multithreading is that it allows performing multiple operations simultaneously without blocking the user. A thread can be in one of five states - new, runnable, running, non-runnable (blocked), and terminated. There are two ways to create threads in Java - by extending the Thread class or implementing the Runnable interface.
Multithreading in Java allows executing multiple threads simultaneously by using lightweight threads that share a common memory area. This saves memory compared to multiprocessing and has lower context switching costs. The main advantage of multithreading is that it allows performing multiple operations simultaneously without blocking the user. A thread can be in one of five states - new, runnable, running, non-runnable (blocked), and terminated. There are two ways to create threads in Java - by extending the Thread class or implementing the Runnable interface.
Multithreading in java is a process of executing multiple
threads simultaneously. Thread is basically a lightweight sub-process, a small unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. Java Multithreading is mostly used in games animation Advantage of Java Multithreading: 1) It doesn't block the user because threads are independent and you can perform multiple operations at same time. 2) You can perform many operations together so it saves time. 3) Threads are independent so it doesn't affect other threads if exception occurs in a single thread. Multitasking: Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways: Process-based Multitasking (Multiprocessing) Thread-based Multitasking (Multithreading) 1) Process-based Multitasking (Multiprocessing) Each process have its own address in memory i.e. each process allocates separate memory area. Process is heavyweight. Cost of communication between the processes is high. Switching from one process to another require some time for saving and loading registers, memory maps, updating lists etc. 2) Thread-based Multitasking (Multithreading) Threads share the same address space. Thread is lightweight. Cost of communication between the thread is low. Life cycle of a Thread (Thread States): A thread can be in one of the five states. But, there are only 4 states in thread life cycle in java new, runnable, non-runnable and terminated. There is no running state. The life cycle of the thread in java is controlled by JVM. The java thread states are as follows: 1. New 2. Runnable 3. Running 4. Non-Runnable (Blocked) 5. Terminated 1. New : The thread is in new state if you create an instance of Thread class but before the invocation of start() method. It remains in this state until the start() method is called on it 2. Runnable : After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task. But the thread scheduler has not selected it to be the running thread. 3. Running: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing. A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs. The thread is in running state if the thread scheduler has selected it. 4. Non-Runnable (Blocked): This is the state when the thread is still alive, but is currently not eligible to run. 5. Terminated : A runnable thread enters the terminated state when it completes its task or otherwise terminates. Create thread: There are two ways to create a thread: 1. By extending Thread class 2. By implementing Runnable interface. Thread class: Thread class provide constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface. Commonly used Constructors of Thread class: Thread() Thread(String name) Thread(Runnable r) Thread(Runnable r, String name) Thank You…