What is multithreading?

What is multithreading? Why is it important to develop computer programs? Discuss life cycle of thread in detail.

Multithreading is the ability to run multiple threads concurrently. Thus multithreaded programs can do many things at once. A thread is a separate unit of execution performing a particular task in a multithreaded program.

A thread is implemented by a particular method in programming language such as Java. With multithreading, we can achieve multitasking. If we have multiple CPUs, then each thread of a program can run on different CPUs allowing parallelism. If we have only one CPU, then the threads take turns in their run and this context-switching of threads takes less time than multiprocessing systems.

For example, in word processors such as MS-Word, one thread may be receiving input, another thread may be performing grammar check, and another thread may be autosaving the data, and so on. While one thread is waiting, another thread is scheduled to run in single CPU systems.

Further, in multithreading, all thethreads of a program share the same memory space whereas if it has been multiprocessing system, then each process would have its own memory space. Thus, multithreading is important in programming for achieving multitasking and for optimizing resource use.

The life cycle of a thread in java is controlled by JVM. The life cycle of a java thread involves the following states in which the thread goes through.

  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.

multithreading

Also View:What-is-a-Private-Cloud-Advantages_-Disadvantages_-and-Types