Beruflich Dokumente
Kultur Dokumente
threads simultaneously.
• Advantages:- 1. Thread share same space.
2. Multiple threads works simultaneously.
3. Cost of communication between processes is
very low.
Thread (class)
Direct implements
extends
MyRunnable
MyThread
(Class)
(class)
Waiting
/joining
New/
born Ready Running Dead
state
waiting
Suspend
1. start() :- ready state
2. run():- running state
3. Yield() :- to give chance to other threads for execution
(running to ready state).
4. Sleep() :- when thread is not assigining any task, then it goes
to sleep state .
5. Join():- waiting state , until other thread completes their
task.
6. wait() :- thread calls waiting state , until another lock get
release.
7. notify():- Thread get notification of release the lock, and
then lock goes to another resource.
8. Suspend():- thread got suspended.
9. Resume() :- again thread goes to start state .
10. Stop() :- thread completes its execution, dead state.
• If we use Runnable interface mechanism then
internally it uses Thread class object to starting the
thread.
• When we want “to apply is – a relationship “ to the
child class , then we should go for Runnable interface
concept.
• Java wont provide support for multiple inheritence ,
means a single class cant extends 2 classes at a same
time.
• To apply thread class behaviour only to the child class
in is-a relationship then we use Runnable(I).
Thread Object
class Runnable(i)
class
Parent
extending class implementing
extending
Child class
• sleep():- to pause the execution of any thread for a
particular time .
• Join():- whenever we need to wait until other threads
to finish its execution , before we can proceed.
• Wait(), notify(),notifyAll():- with the help of this
methods thread can communicate about the lock
status of a resource.
• Threadlocal:- Java thread provides ThreadLocal utility
class. to create thread local variable at class level.
• ThreadDump:- It provides useful info. to analyze
performance issue with the application.
• Callable:- 1. use to define thread.
2. Having call()
3. Introduced in 1.5 v.
4. Returns result.
5. Throws Checked Exception.
• Runnable:- 1. use to define thread.
2. Having run()
3. Introduced in 1.0 v.
4. Doesn’t returns any result.
5. Doesn’t throw any checked exception.
• Synchronous:-
1. Only one thread can access a object at a time.
2. Thread safety.
3. Other threads are in waiting ,so performance
issue is there.
• Asynchronous:-
1. Multiple threads can access a object at a time.
2. No thread safety.
3. No thread is in waiting state so performance is
high.
There are two ways
• Extends thread class.
• Implements Runnable interface.
(use runnableinterface concept when we want to
use is – a relationship in class.)