Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday, 26 March 2017

Multithreading in Java

 What is Multithreading in Java ?

Java multithreading is a  process of executing more than one thread simultaneously is known as multithreading in java.

The main purpose of multithreading is to achieve the concurrent execution.


Advantage of Multithreading in Java

  • Save Time : By using multi-threading in java you can perform multiple operation at the same time so it saves time.
  • Doesn't Block Users : Java multithreading doesn't block the user because thread are independent, if exception occurs in one thread it does not affect other thread so you can perform multiple operation at the same time.

Thread

Thread in java is a light weight process and thread is a small part of a process or program. It is used to achieve multitasking.

Threads have separate path of execution and threads are independent if exception occurs in one thread, it does not affect other threads.

Thread share common memory area.


Thread in Java

There is at least one process is required for each thread. In the above picture, thread is executed inside the process. There is context-switching between these threads(T1, T2 and T3 threads). There can be multiple process inside the OS(operating system) and can be multiple thread in one process. 


Multitasking in Java

Executing multiple task or more than one task at the same time is called multitasking.


Real life example of Multitasking 

There are real life example of multitasking in given below picture

                                        Task 1 - Speaking on phone
                                        Task 2 - Writing note 


Multitasking Image



Example 2: We are able to play the music in our computer and still be able to work on a document
            
Task 1 - Play music on computer or  laptop
Task 2 - Work on notepad, Microsoft Word, etc.

Why use multitasking

  • We use multitasking to save time.
  • We use multitasking to utilize the CPU.

How to achieve multi-tasking 

You can achieve multitasking by two ways, these are :

  1. Process-based multitasking (Multiprocessing)
  2. Thread-based multitasking (Multithreading)

(1) Process-based Multitasking (Multiprocessing)

  • Process is heavy weight.
  • Running state of an instruction is call process.
  • Process taking separate memory area in a ram.
  • Between the process, the cost of communication is very high.
  • Switching from one process to another require some time for saving and loading registers, updated list, etc.

(2) Thread-based Multitasking (Multithreading)

  • Thread is a small part of a program or process.
  • Thread is a lightweight process.
  • Thread share common memory area in a ram.
  • Between two thread, the cost of communication is very low.

Multitasking can be achieved by two ways through multiprocessing and multithreading but we use multithreading not multiprocessing because thread is lightweight than process and thread share common memory area whereas process taking separate address space in memory.

So we are going to use thread-based multitasking to achieve multitasking in java.

Multithreading is mostly used for the game, animations, etc.

In java multi-threading, we will learn many concepts like How to create thread in java, How to sleep a thread in java, How to join a thread with another thread, What is thread scheduler in java thread and more.
Share:

5 comments:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate