Multithread

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Multithreading

1. Program:- is a set of Ins, is a passive entity, has ability to run(Execute)


2. Process: - execution phase of PRG is denoted as process.
3. Multiprogramming:-Multiple PRGS inside RAM, waiting for the CPU
4. Multitasking:- CPU executing PRGS at the same time literally (based on
some scheduling algos)
5. Multiprocessing:- Many CPU executing many PRGS at the same time
(Actually), also known parallel computing.

Threads:-A part of process, bole toh a sub-process, a process may have


multiple threads, they executes in parallel (at same time), each thread has
its own designated task, and they share same address space of process, so
by using a thread we can do many jobs inside a process.
As compare to process tread is light weight entity ( So require less
resources)

For java treads are objects they live inside in heep the thread can be
implemented by using thread class or by using runnable interface.
Methods of thread class:
1. Void run():- actual task to be performed by thread(business logic)
2. Void start():- It is used to start thread (internally start() method
invokes run() method)
3. Boolean is Alive():- it will return true if thread is started .

How to implement thread:-


Syntax:-
1. Create a class that inherits Thread class.
2. Override run() method inside subclass.
3. Create object of subclass and class start() method on it.

Note:- The JVM will execute main() method and run() method
at the same time.
(Output will be unpredictable)

When a java application starts the JVM creates a thread known as main thread That
becomes responsible to execute the main method.(So the main method
internally denoted by a thread, every java application has at list one
thread main thread.) Every thread has a name priority and group name.
The thread with a higher priority may be given a chance to complete
execution first (But is not gureented)
The Preemptive approach is followed.
[Acquire CPU-> use it -> release it] and repeat the process.

Name:- Thread-NV NV >=0 (NV-Numeric Value)


Priority:- 1 to 10 [5 default]
Group name:- main
Group is main

Method 4:- void setName(String name) it is used to set the name of thread
Note:- If name is null exception will arise.

5._ String getName():- It will return the name of thread.

6. void setPriority(int pri):- It is used to set the priority of thread .

Note:- pri>=1 && pri<= 10 else exception

7. int getPriority(): It will return the priority of thread.

8. static Thread currentThread():- it will return the object current thread.

Daemon thread:- They run in background and provides services for other
threads. As soon as the service receiver terminate they will also terminate. For
the user define thread are non daemon, Main thread is also non Daemon.

Example of daemon thread is garbage collector it will de-allocated the memory


(still in the background ) for the unused object.

Method 9:- boolean isDaemon():- This method will return true if thread is
daemon thread otherwise false.

10. void steDaemon(boolean b):- by using this method a thread will become a
daemon thread (dependent thread).

Note:- it is mandatory to call this method before start() otherwise a thread will
remain non-daemon.

The Life of Thread:- from to end life cycle of thread can be denoted by using
five stages ,

1. New
2. Ready-to run
3. Running
4. Blocked(wait)
5. Terminated
1. New:- When the create of object of thread it is assumed as a new
stage, multiple threads can be in this stage. The isAlive method return
false in new stage. When we call start method on a thread it will be
shifted from new to RTR stage. Multiple thread can be in this stage.
RTR means thread are waiting to be dispatch to the CPU.
Running:- Running means the execution state of a thread only one
thread can be in running stage. Running means the statements of run
method is executing. The JVM select a thread from RTR state and
assign CUP for it. The selection will be done by the JVM by using
some predefine scheduling algorithms (Generally Round robin) (Time
quantum base). A thread will be shifted from running to block in
following scenario:-
A thread will be shifted from RTR to block following scenario:-
6. Termination:- My be denoted normally and abnormally
Normally:- If all the statement of run method executed without any
exception and handle exception.
Abnormally:- If any unhandled exception.
Method 11. Void join():- This method use for inter thread collaboration,
by using this method a thread says to currently executing thread let me
execute first completely, then you may be resume execution.
12. void join(long time):- This same of above but the timer can be
specify.
13. static void sleep(long millis):- By using this method a currently
executing thread may paused it’s execution. IN this case a thread will
release the resources(Known as blocks, monitor, mutates) those are
occupied by this thread.
14. static void sleep(long milis, int nanos):- It is same of above
Millis>=0 and nano>=0 && nanosn<=999999

You might also like