Multithreading in Java Multithreading is a process of executing - TopicsExpress



          

Multithreading in Java Multithreading is a process of executing multiple threads simultaneously. Thread is basically a lightweight subprocess, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. But we use multithreading than mulitprocessing because threads share a common memory area. They dont allocate separate memory area so save memory, and context-switching between the threads takes less time than processes. Multithreading is mostly used in games, animation etc. ________________________________________ 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 process 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. • Note:At least one process is required for each thread. ________________________________________ What is Thread? A thread is a lightweight subprocess, a smallest unit of processing. It is a separate path of execution. It shares the memory area of process. As shown in the figure, thread is executed inside the process. There is context-switching between the threads. There can be multiple processes inside the OS and one process can have multiple threads. Note:At a time only one thread is executed. Life cycle of a Thread (Thread States) A thread can be in one of the five states in the thread. According to sun, there is only 4 states new, runnable, non-runnable and terminated. There is no running state. But for better understanding the threads, we are explaining it in the 5 states. The life cycle of the thread is controlled by JVM. The 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. 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.
Posted on: Sun, 19 Jan 2014 13:11:39 +0000

Trending Topics



Recently Viewed Topics




© 2015