Multithreading refers to two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class. A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously.
Multithreading has several advantages over Multiprocessing such as;
- Threads are lightweight compared to processes
- Threads share the same address space and therefore can share both data and code
- Context switching between threads is usually less expensive than between processes
- Cost of thread intercommunication is relatively low that that of process intercommunication
- Threads allow different tasks to be performed concurrently.
Life cycle of a Thread
- New : A thread begins its life cycle in the new state. It remains in this state until the start() method is called on it.
- Runable : After invocation of start() method on new thread, the thread becomes runable.
- Running : A method is in running thread if the thread scheduler has selected it.
- Waiting : A thread is waiting for another thread to perform a task. In this stage the thread is still alive.
- Terminated : A thread enter the terminated state when it complete its task.
No comments:
Post a Comment