100% found this document useful (1 vote)
304 views14 pages

Multithreading

Multithreading allows executing multiple threads simultaneously by sharing resources like memory. There are three ways to define a thread in Java: extending the Thread class, implementing the Runnable interface, or implementing the Callable interface. The Runnable interface is commonly used as it allows applying thread behavior without multi-inheritance issues and implements the run() method for the thread. Thread states include new, ready, running, waiting, suspended, and dead.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
304 views14 pages

Multithreading

Multithreading allows executing multiple threads simultaneously by sharing resources like memory. There are three ways to define a thread in Java: extending the Thread class, implementing the Runnable interface, or implementing the Callable interface. The Runnable interface is commonly used as it allows applying thread behavior without multi-inheritance issues and implements the run() method for the thread. Thread states include new, ready, running, waiting, suspended, and dead.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

• Multithreading is a process of executing multiple

threads simultaneously.
• Advantages:- 1. Thread share same space.
2. Multiple threads works simultaneously.
3. Cost of communication between processes is
very low.

Multitasking:- multiple task execution


simultaneously. Like listening music,
programming, etc.
Multithreading:- multiple threads , thread with I a
single program , a single unit, executes
simultaneously, with in a program .
Process vs Thread
• There are total 3 ways to define a thread in
java.
1. Extends Thread class.
2. Implements Runnable interface.
3. Implements callable interface.
Define a Thread
Present in java.lang package

RUNNABLE Contains only one


(INTERFACE) method
Run()
Internally implements

Thread (class)
Direct implements

extends

MyRunnable
MyThread
(Class)
(class)

1st Approach 2nd Approach


• Ex:-
Class MyThread extends Thread
{ public void run() // overridden method
{ for(int i=0; i<10; i++)
{ sopln(“child thread”); }
}
}
class ThreadDemo
{ psvm(s[] args)
{ Mythread t= new MyThread(); // thread instantiation
t.start(); //starting of thread
for (int i=0; i<10; i++)
{ sop(“main thread”) ; }
}
}
• Ex:-
class MyRunnable implements Runnable
{ public void run()
{ for(int 1=0; i<10; i++)
{ sopln (“child thread”) ; }
}
}
class ThreadDemo
{ psvm(s[] args)
{ MyRunnable r= new MyRunnable();
Thread t= new Thread(r); // to start the thread we use thread class.
t.start(); // to start the thread
for (int i=0; i<10; i++)
{ sopln(“main thread”); }
}
}
Sleeping

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.)

You might also like