Threads
What is Thread: A
thread is a lightweight sub process, a smallest unit of processing. It is a
separate path of execution. It shares the memory area of process.
Multitasking
Multitasking is a process of executing multiple
tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can
be achieved by two ways:
·
Process-based
Multitasking(Multiprocessing)
· Thread-based Multitasking(Multithreading)
Note: Each processor is independent.
Multithreading is a process of executing
multiple threads simultaneously
Achieving multiple threads in single class
1) Process-based Multitasking (Multiprocessing)
Ø Each process have its
own address in memory i.e. each process allocates separate memory area.
Ø Process is
heavyweight.
Ø Cost of communication
between the process is high.
Ø Switching from one
process to another require some time for saving and loading registers, memory
maps, updating lists etc.
|
2) Thread-based Multitasking (Multithreading)
· Threads share the same
address space.
· Thread is lightweight.
· Cost of communication
between the thread is low.
· At least one process
is required for each thread.
All programming languages
support multithreading
Difference :-
|
Multiprocessing and multithreading, both are
used to achieve multitasking.
But we use multithreading than multiprocessing
because threads share a common memory area. They don't allocate separate
memory area so save memory, and context-switching between the threads takes
less time than processes.
|
Multithreading is mostly used in games, animation etc.
Examples :
In system opening command window, notepad, eclipse, only
attached files there are multiple independent.
Every system has won processor that is take care of entire
process.
Multithreading and
explain its application areas
Executing several threads
simultaneously where each thread is a separate independent part of the same
program is called multithreading. Java language provides inbuilt support for
multithreading by defining a reach library, classes and interfaces like
Thread, Thread Group, Runnable etc. The main important application area of
multithreading are video games implementation, animation development,
multimedia graphics etc
Advantage
of Multithreading :
·
The main
advantage of multithreading is reduces response time and improves performance
of the system.
·
Java language provides inbuilt support for
multithreading by defining a reach library, classes and interfaces like
Thread, Thread Group, Runnable etc. But in C++ there is no inbuilt support
for multithreading
|
There two types Threads:
1. User thread
2. Daemon thread
User threads:
It is independent thread it’s not dependent on parent thread
User threads continually execute rather than looking for
parent thread once assigned task is fully got over then only come again
Daemon thread:
Ø It is dependent thread its dependent on parent thread.
Ø The
daemon thread is a service provider thread.
Ø It
provides services to the user thread.
Ø Its
life depends on the user threads i.e. when all the user threads dies, JVM terminates
this thread automatically
Important
points:
- It provides services to user threads for
background supporting tasks. It has no role in life than to serve user
threads.
- Its life depends on user threads.
- It is a low priority thread.
Thread
class: In java there two ways creating threads
Thread class provide constructors and methods
to create and perform operations on a thread, Thread class extends Object
class and implements Runnable interface.
There
are two ways to create a thread:
While executing java command internally three
threads are creating these are
The Thread Scheduler: The thread scheduler is the part of the JVM
that decides which thread should run. There is no guarantee that which run
able thread will be chosen to run by the thread scheduler. Only one thread at
a time can run in a single process. The thread scheduler mainly uses
preemptive or time slicing scheduling to schedule the threads
What is the difference between preemptive scheduling and time
slicing?
Under preemptive scheduling, the highest
priority task executes until it enters the waiting or dead states or a higher
priority task comes into existence. Under time slicing, a task executes for a
predefined slice of time and then reenters the pool of ready tasks. The
scheduler then determines which task should execute next, based on priority
and other factors
Garbage collector:
All abounded objects are
removing from the memory in finally.
Main() :
Its responsible to
execute main method .it’s a user thread
|
Note:
Garbage collector and main are registered
with thread Scheduler
No comments:
Post a Comment