what is multithreading in java - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

what is multithreading in java

Multitasking

Executing several tasks simultaneously is the concept of multitasks.

 

There are two types of multitasking

1) Process based multitasking
2) Thread based multitasking/multi-threading

 

thread vs process

Process based multitasking

Executing several tasks simultaneously where each task is a separate independent program(process) is called process-based multitasking.

  • Each process has an address in memory.
  • Process based multitasking is best suitable at OS level.
  • Process is heavy weight.
  • Cost of communication between the process is high.
  • Switching from one process to another required some time for saving and loading registers, Memory mapping and updating lists etc.!

 

Example:

while typing a Java program in editor we can listen audio songs from same system at the same time, we can download a file from internet all these tasks will be executed simultaneously and independent of each other hence it is process based multitasking.

 

 

Thread based multitasking/ Multithreading

Executing multiple threads simultaneously on a single process is called multithreading.

  • A thread is light weight sub process.
  • Thread shares the same address space.
  • They don’t allocate separate memory, so it saves memory. Also, context switching between threads takes less time.
  • Each program has at least one thread that is called main thread.
  • The main thread starts from beginning of main method and ends when main method execution is completed.
  • Each thread is created after the main thread because JVM starts from the main method.
  • A program can have any number of threads and each thread has priority number that helps the JVM to prioritization of the threads.
  • Thread based multitasking is best suitable at programmatic level.

 

Example:

While using word pad we can edit the text, at the same time spell checker working on parallel like these we can perform multiple operation on single object with the help of multithreading.

 

Advantage of multithreading

  • It doesn't block the user because threads are independent, and you can perform multiple operations at the same time.
  • You can perform many operations together, so it saves time. Highest Salary in SQL
  • Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread.

 

No comments:

Post a Comment