Threads
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.
Computer
Music Player
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.
Runnable
Terminated
NewTimed Waiting
WaitingProgram starts thread
Unlock signal Thread completes task
Await lock
Await sleep Interval express
Thread – Life Cycle
Thread – Methods
New
Ready
Blocked
Running
Terminated
t.notify()
notifyAll()
t.start()
t.run()Run method ends
t.sleep(),t.wait()
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.
Creation of threads in multiple ways
Threads can be created in two ways.• Implementing Runnable interface.• Extending Thread class
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(); }}
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(); }}
Thread Class methods
• int getPriority()• void setPriority(int newpriority)
• void sleep(long millis)• void join(long millis)
• Thread currentThread()• Thread getState()
• void setName(String name)• String getState()
• void start()• void run()
• void wait()• void notify()• void notifyAll()
• void stop()• void yield()
Multithreading
• When two or more threads are running in a process simultaneously then it is called Multithreading.
Multithreading Example
package com.edureka.multithreading;public class Second extends Thread{public void run(){
System.out.println("This is first thread");}
}
package com.edureka.multithreading;public class Second extends Thread{public void run(){
System.out.println("This is second thread");}
}
Multithreading Example
package com.edureka.multithreading;public class Main extends Thread{public static void main(String[] args){
Thread thread1 = new Thread(new One());Thread thread2 = new Thread(new Second());
thread1.start();thread2.start();
}}
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’.
How to synchronize the object?
Synchronized method
synchronized(object){
//Statements}
synchronized returntype methodname(){
//Statements}
Synchronized block
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()
Deadlock
• Deadlock is a situation in which a thread has got an object and waiting for an object locked by another thread which is waiting for the object locked by first thread.
Thread1Thread2
Object1
Object2
waiting
waiting
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.
•Q& A..?
Thanks..!