Professional Documents
Culture Documents
Multithread
Multithread
Multithread
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 .
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.
Method 4:- void setName(String name) it is used to set the name of thread
Note:- If name is null exception will arise.
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.
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