Easy learning with example program codes

Multithreading in java


Multitasking is a way of executing multiple tasks during the same period of time. Multiple tasks use common resources like CPU and main memory.

With a single CPU only one task can be running at one point of time, so CPU uses context switching to perform multitasking. Context switch (Context means state) is the process of storing and restoring the state of a process so that execution can be resumed from the same point at a later time.

Types of Multitasking:

     1. Multiprocessing.
     2. Multithreading.


Multiprocessing is a type of multitasking based upon processes i.e. context switching is done in-between processes. For example: Typing in notepad, Listening music and downloading file from internet at the same time. Here in example we can clearly see that all applications are independent. Multiprocessing is the type of multitasking which is handled at operating system level.


A process has a self-contained execution environment i.e. allocates separate memory area. Context switch time is more in case of processes because switch is done between different memory areas.


Multithreading is a type of multitasking based upon threads i.e. context switching is done in-between threads. In case of multithreading, multiple independent tasks are executed simultaneously. These independent tasks are the part of same application. Multithreading is the type of multitasking which is handled at program level.


A thread is a lightweight process. Thread uses process’s execution environment i.e. memory area. Context switch time is less in case of threads because switch is done within the same process’s memory area.

A thread represents a flow of execution. Every thread has a job associated with it.

Note: A thread can’t be exist without process, it exist within the process.

Difference between process and thread in java:

                       Process                          Thread
  1. Process has its own main memory for execution.
  2. Process is considered as heavyweight component.
  3. One process can have multiple threads.
  4. Context switch time is more.
  1. Thread use process’s main memory for execution and share it with other threads.
  2. Thread is considered as lightweight component.
  3. One thread can’t have multiple process.
  4. Context switch time is less.

Next Topic: Thread life cycle in java.
Previous Topic: Commonly used exception methods of Throwable class in java.

Related Topics:

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki