What is Multitasking and Multithreading?
In computing, multitasking is a method by which multiple tasks, also known as processes, share common processing resources such as a CPU. With a multitasking OS, such as Windows XP, you can simultaneously run multiple applications. Multitasking refers to the ability of the OS to quickly switch between each computing task to give the impression the different applications are executing multiple actions simultaneously.
Multithreading extends the idea of multitasking into applications, so you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application. Multithreading enables you to write in a way where multiple activities can proceed concurrently in the same program.
Life Cycle of a Thread
Thread undergoes different stages in its life time. The stages of the thread are as follows.
A thread that has not yet started is in this state.
A thread executing in the Java virtual machine is in this state.
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
A thread that has exited is in this state.
A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.
Assigning Priorities To Thread: Suppose a process has a multiple no.of threads, it may be difficult for cpu to schedule these threads so, we can set priority to these threads which makes it easy for cpu to schedule these threads.
There are three priorities for a thread