Multithreading

MULTITHREADING IN JAVA

0  0 2016-09-28 02:52:35

Multithreading is a process of execute more than one threads at a time.
Ex. listening music in music player and typing in ms word.

Thread is a sub process of a process.
It is basically a lightweight sub-process of process and a smallest unit of processing. Multithreading are used to achieve multitasking.
Threads are using common memory area in a process so no need to allocate separate memory area. Hence they are saving memory.
Java Multithreading is using in games, animation etc.
Advantages of Multithreading in Java
1) Every thread is independent so we can perform multiple operations at same time.
2) it saving our time because we can perform many operations together  at time.
3) if exception occurs in a single thread then it doesn't affect other threads

Multitasking
Multitasking is a process of execute more than one at a time. We are using multitasking to utilizing the CPU. 
We can achieve it by two ways:
1. Process based Multitasking(Multiprocessing)
2. Thread based Multitasking(Multithreading)

1) Process based Multitasking (Multiprocessing)
1. Each process has its own address in memory ex. each process allocates separate memory area.
2. Process is heavy weight.
3. Cost of communication between the process is high.
4. Switching from one process to another require some time for saving and loading registers, memory maps, updating lists etc.

2) Thread based Multitasking (Multithreading)
1. Threads share the same address space.
2. Thread is lightweight.
3. Cost of communication between the thread is low.

Thread in java
Thread is a sub process of a process.
It is basically a lightweight sub-process of process and a smallest unit of processing.
Threads are using common memory area in a process so no need to allocate separate memory area. Hence they are saving memory. 
Thread doesn't affect other threads if there occurs exception in one thread so it is independent sub process.
It has separate path of execution in process. 

 

Life cycle of a Thread:
1. New
2. Runnable
3. Running
4. Blcaked
5. Terminated(dead)
 
1. New State   : First state is new state. When we create an instance of thread class then it is in new state before the invocation of start () method
2.  Runnable   : when we call start() method with thread object then this state is in runnable state. 
3. Running      : When we select thread scheduler then it is in running state. 
4. Blocked     : It is on sleep mode.
5) Terminated : It is dead  state when run() method exits. 

Comments (0)

Post Your Comments

Headquarter



Hong Kong Office



Find Us On

Ziuby
Ziuby