• August 29, 2014
  • Core Java
  • Comments Off on Multithreading in java

Multithreading in java

Multitasking:

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:

Multiprocessing is a type of multitasking based upon processes i.e. context switching is done in-between processes.

Process:

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:

Multithreading is a type of multitasking based upon threads i.e. context switching is done in-between threads.

Thread:

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

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

Difference between process and thread in java:

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

Multithreading in java.
Thread life cycle in java.
Way of creating thread in java.
Commonly used methods of Thread class.
Thread scheduling in java with example.
Thread priority in java with example.
Naming a thread in java with example.
Joining a thread in java with example.
Daemon thread in java with example.
Can we start a thread twice?
Can we call run method directly?
Difference between Thread.yield() and Thread.sleep() methods.
Deadlock in java with example.
Starvation in java with example.
Inter-thread communication in java with example.
Synchronization in java with example.
Synchronized method in java with example.
Static synchronization in java with example.
Synchronized block in java with example.

 


Comments are closed.