2. Thread
• A thread is a part of execution in a program.
• The Java Virtual Machine allows an application to have
multiple threads of execution running concurrently.
3. Computer
Playing Music
Other Video Player
Tasks
My Super Browse
Computer Internet
NotePad
Downloading Playing
Files Game
4. Music Player
Playing Managing
Songs Playlists
Music
Player
Downloading Accessing
Updates Internet
5. Why do we use threads?
• Thread is a light weight
process as it shares the
same memory and the
resources of the
process in which it is
created.
6. Thread – Life Cycle
New
Program starts
thread
Unlock signal Thread
completes task
Waiting Runnable Terminated
Await lock
Await sleep Interval express
Timed
Waiting
7. Thread – Methods
New Blocked
t.sleep(),
t.start() t.wait()
Run method
ends
t.run()
Ready Running Terminated
8. Thread Priorities
• When multiple threads are running the order of
execution of threads depend on priority given to
thread.
• Priority values ranges from 1 to 10.(default-5).
• setPriority(int priority) – method to set priority.
9. Creation of threads in multiple ways
Threads can be created in two ways.
• Implementing Runnable interface.
• Extending Thread class
10. Implementing Runnable Interface
package com.edureka.threadsrunnable;
public class ThreadClass implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
} package com.edureka.threadsrunnable;
public class Main{
public static void main(String[] args) {
ThreadClass obj = new ThreadClass(); //object of class ThreadClass
Thread thread_runnable = new Thread(obj);
thread_runnable.start();
}
}
11. Extending Thread Class
package com.edureka.threads;
public class ThreadClass extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
} package com.edureka.threads;
public class Main{
public static void main(String args[]) {
ThreadClass obj = new ThreadClass(); //object of class ThreadT
Thread thread_extend = new Thread(obj);
thread_extend.start();
}
}
13. Multithreading
• When two or more threads are running in a
process simultaneously then it is called
Multithreading.
14. Multithreading Example
package com.edureka.multithreading;
public class Second extends Thread{
public void run()
package com.edureka.multithreading;
{
public class Second extends Thread{
System.out.println("This is
public void run()
first thread");
{
}
System.out.println("This is
}
second thread");
}
}
16. Synchronization
• The process of making only one thread access
the object when two or more threads are
accessing the same object is called
Synchronization.
• Keyword - ‘synchronized’.
17. How to synchronize the object?
synchronized(object)
{ Synchronized
//Statements
} block
synchronized returntype methodname()
Synchronized {
method //Statements
}
18. Inter-Thread Communication
• The process of communication between multiple threads is
called Inter-thread communication.
• In this process of communication when one thread is
executing the other thread will wait for the thread to be
executed.
• The methods used for communication are:
wait()
notify()
notifyAll()
19. Deadlock
Object
• Deadlock is a situation in 1
waiting
which a thread has got an
object and waiting for an
object locked by another Thread2
thread which is waiting for Thread1
the object locked by first
thread.
waiting Object
2
20. Deadlock Example
• Player1 has got a bat to play and waiting for a
ball.
• Player2 has got a ball to play and waiting for a
bat.
• Both are waiting for the resource which are
held by the other player. This leads to
deadlock.