Posts

Showing posts from January, 2021

Life cycle of a Thread and creating thread class

Image
  Life cycle of a Thread /Thread States:         A thread can be in one of the five states. According to sun, there is 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: New Runnable Running Non-Runnable (Blocked) Terminated 1) New The thread is in new state if you create an instance of Thread class but before the invocation of start() method. 2) Runnable The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread. 3) Running 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 thread is in terminated or dead state when its run() method exits.     Create thread:- There are two ways to

Multithreading

Image
    Multithreading :-    Multithreading in java  is a process of executing multiple threads simultaneously. A A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multi threading, both are used to achieve multitasking. However , we use multithreading than multiprocessing because threads use a shared 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, etc.     Advantages of Java Multithreading:- 1) It doesn't block the user because threads are independent and you can perform multiple operations at the 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 an exception occurs in a single thread. Multitasking Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to u