Friday, October 16, 2015

Multi Threading in Java

Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.
A multithreading is a specialized form of multitasking. Multitasking threads require less overhead than multitasking processes.
I need to define another term related to threads: process: A process consists of the memory space allocated by the operating system that can contain one or more threads. A thread cannot exist on its own; it must be a part of a process. A process remains running until all of the non-daemon threads are done executing.
Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum.


Difference Between Process and Thread:

1.Threads share the address space of the process that created it;
 processes have their own address.

2.Threads have direct access to the data segment of its process; 
processes have their own copy of the data segment of the parent process. 

3.Threads can directly communicate with other threads of its process; 
processes must use interprocess communication to communicate with sibling processes(child process) 

4.Threads have almost no overhead; processes have considerable overhead.
(Overhead related to context switching)

5.New threads are easily created; new processes require lot of new allocation
 by operating System

6.Threads can exercise considerable control over threads of the same process; 
processes can only exercise control over child processes. 

7.Changes to the main thread (cancellation, priority change, etc.) may affect the 
behavior of the other threads of the process; 
changes to the parent process does not affect child processes. 

No comments:

Post a Comment