4-Multithreaded Programming-05Sep24
4-Multithreaded Programming-05Sep24
MULTITHREADED
PROGRAMMING
(cont..)
1
Topics of today!!!
Interthread Communication
Deadlock
2
Interthread Communication
Inter-thread communication or Co-operation
is allowing synchronized threads to
communicate with each other
A mechanism in which a thread is paused
running in its critical section and another
thread is allowed to enter (or lock) in the
same critical section to be executed
3
Interthread Communication (cont..)
It is implemented by the following methods
of Object class
wait( )
notify( )
notifyAll()
All the above three methods can be called
only from within a synchronized context
4
Interthread Communication (cont..)
wait( )
Causes current thread to release the lock
and wait until either another thread
invokes the notify() method or the
notifyAll() method for this object, or a
specified amount of time has elapsed
public final void wait( ) throws InterruptedException
public final void wait(long timeout) throws
InterruptedException
5
Interthread Communication (cont..)
notify( )
Wakes up a single thread that is waiting on
this object's monitor
Any threads are waiting on this object, one
of them is chosen to be awakened
public final void notify()
6
Interthread Communication (cont..)
notifyAll( )
Wakes up all threads that are waiting on
this object's monitor
public final void notifyAll()
7
Interthread Communication (cont..)
Understanding the process of inter-thread
communication
1. Threads enter to acquire lock
2. Lock is acquired by on thread
3. Now thread goes to waiting state if you call wait() method
on the object. Otherwise it releases the lock and exits
4. If you call notify() or notifyAll() method, thread moves to
the notified state (runnable state)
5. Now thread is available to acquire lock
6. After completion of the task, thread releases the lock and
exits the monitor state of the object
8
Interthread Communication (cont..)
Understanding the process of inter-thread
communication
9
Interthread Communication (cont..)
Example
class Chat {
boolean flag = false;
public synchronized void question(String msg) {
if (flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(msg);
flag = true;
notify();
} 10
Interthread Communication (cont..)
public synchronized void answer(String msg) {
if (!flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(msg);
flag = false;
notify();
}
}
11
Interthread Communication (cont..)
class QuestionThread implements Runnable {
Chat m;
String[] s1 = { "Hi", "How are you ?", "I am also doing
fine!" };
public QuestionThread (Chat m1) {
this.m = m1;
new Thread(this, "Question").start();
}
public void run() {
for (int i = 0; i < s1.length; i++) {
m.question(s1[i]);
}
}
}
12
Interthread Communication (cont..)
class AnswerThread implements Runnable {
Chat m;
String[] s2 = { "Hi", "I am good, what about you?", "Great!" };
14
Deadlock
A special type of error that need to avoid that
relates specifically to multitasking is deadlock
occurs when two threads have a circular
dependency on a pair of synchronized
objects
15
Deadlock – Example (cont..)
public class Deadlock {
final static String R1 = "Hello Welcome to PSGCT!";
final static String R2 = "Visit PSGCT!";
public static void main(String[] args) {
Thread T1 = new Thread() {
public void run() {
synchronized (R1) { System.out.println("Thread T1 locked -> Resource R1");
synchronized (R2) {
System.out.println("Thread T1 locked -> Resource R2");
} } } };
Thread T2 = new Thread() {
public void run() {
synchronized (R2) { System.out.println("Thread T2 locked -> Resource R2");
synchronized (R1) {
System.out.println("Thread T1 locked -> Resource R1");
} } } };
T1.start();
T2.start();
}
16
}
Deadlock – Example (cont..)
• In the above code snippet, declared the strings R1 and R2 as
resources. In the main method, two threads have created, T1 and
T2. In thread T1, run method implemented to lock the resource R1
using the synchronized method. Similarly, in thread T2, lock the
resource R2.
17
Deadlock – Conditions
Mutual Exclusion - resources can be shared with
only one thread at a time
Hold and Wait - a particular thread holds a non-
shareable resource and requests another non-
shareable resource
No Preemption - a thread can voluntarily release
its resources with no fixed schedule
Circular Wait - when one or more threads wait
in a circular order for the resources they require
18
Deadlock – Avoid
Avoid Nested Locks
Avoid Unnecessary Locks
Using Thread Joins (Thread.join())
19
References
https://www.javatpoint.com/inter-thread-communication-
example
https://www.geeksforgeeks.org/inter-thread-
communication-java/
https://www.tutorialspoint.com/java/java_thread_communic
ation.htm
https://docs.oracle.com/javase/tutorial/essential/concurren
cy/deadlock.html
https://www.scaler.com/topics/deadlock-in-java/
20