Multithreading in Java

Multithreading in Java

Using Java we can develop multi-threaded program i.e. Java is a multi-threaded programming language . Multithreading is multiple paths in a program. Due to multiple parts of program are concurrently executed. The aim of multithreading is to achieve the concurrent execution.
A thread is a lightweight sub process, a smallest unit of processing. A thread represent a separate path of execution of a group of statements in a java program, if we write a group of statements are executed by JVM one by one. This means that in every java program there is always a thread running internally.
A thread represents execution of statements. The ways the statements are executed is of two types:
Single tasking
Multi tasking

Single tasking

In single tasking environment, only one task is given to the processor at a time. Let us see, if a programmer typing his first program. It may be take some 10 minutes. When he is typing his program, the micro processor is sitting idle. After completed program, it gives the program to the processor.
In single tasking, only one task is given to the processor at a time. This means we are wasting a lot of processor time and micro processor has to sit idle for a long time. This is drawback in single tasking.

Multi tasking

Multitasking is a process of executing multiple tasks simultaneously i.e. to use the processor time in an optimum way, we can give it several jobs at a time. This is called multi tasking. Multitasking can be achieved by two ways:
1.Process-based Multitasking (Multiprocessing)

  • Each process allocates separate memory area that means each process have its own address in memory.
  • Cost of communication between process is high.
  • Process is heavyweight.
  • Switching from one process to another require some time for saving and loading registers, updating, etc.

2. Thread-based Multitasking (Multithreading)

  • Thread is lightweight.
  • Threads share the same address space.
  • Cost of communication between the thread is low.

 

Leave a Reply

Your email address will not be published. Required fields are marked *