4-Multithreaded Programming-29Aug24
4-Multithreaded Programming-29Aug24
MULTITHREADED
PROGRAMMING
(cont..)
1
Topics of today!!!
Thread Priority
What is Synchronization?
Implementation of Synchronization
2
Thread Priority
Thread scheduler to decide when each thread
should be allowed to run
A higher-priority thread can also preempt a
lower-priority one
Priority can either be given by JVM/given by
programmer explicitly
Accepted value of priority for a thread is in
range of 1 to 10
3
Thread Priority (cont..)
There are 3 static variables defined in Thread
class for priority
public static int MIN_PRIORITY - This is minimum
priority that a thread can have. Value for this is 1
public static int NORM_PRIORITY - This is default
priority of a thread if do not explicitly define it.
Value for this is 5
public static int MAX_PRIORITY - This is maximum
priority of a thread. Value for this is 10
4
Thread Priority (cont..)
The following methods used to set the priority
and to obtain the current priority
final void setPriority(int level)
final int getPriority( )
5
Thread Priority (cont..) - Example
class ThreadDemo extends Thread {
public static void main(String[] args) {
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
ThreadDemo t3 = new ThreadDemo();
System.out.println("t1 thread priority: "+ t1.getPriority());
System.out.println("t2 thread priority: "+ t2.getPriority());
System.out.println("t3 thread priority: "+ t3.getPriority());
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
6
Thread Priority (cont..) - Example
System.out.println("t1 thread priority: "+ t1.getPriority());
System.out.println("t2 thread priority: "+ t2.getPriority());
System.out.println("t3 thread priority: "+ t3.getPriority());
Thread.currentThread().setPriority(10);
System.out.println( "Main thread priority : "
+ Thread.currentThread().getPriority());
}
}
7
What is Synchronization?
Capability to control the access of multiple
threads to any shared resource
Avoid thread interference & consistency
problem
Ensure that resource will be used by only one
thread at a time
synchronized keyword in java creates a block
of code referred to as critical section
8
What is Synchronization? (cont..)
key to synchronization is the concept of the
monitor (semaphore)
a monitor is an object that is used as a
mutually exclusive lock (mutex)
only one thread can own a monitor at a given
time
when a thread acquires a lock, it is said to
have entered the monitor
9
What is Synchronization? (cont..)
all other threads attempting to enter the
locked monitor will be suspended until the
first thread exits the monitor
other threads are said to be waiting for the
monitor
a thread that owns a monitor can reenter the
same monitor if it so desires
10
Example - Without synchronization
class First{
public void display(String msg) {
System.out.print ("["+msg);
try {
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
System.out.println ("]");
}
}
11
Example - Without synchronization (cont..)
class Second extends Thread{
String msg;
First fobj;
Second (First fp,String str){
fobj = fp;
msg = str;
start();
}
public void run(){
fobj.display(msg);
}
}
12
Example - Without synchronization (cont..)
public class WithoutSynMethod{
public static void main (String[] args){
First fnew = new First();
Second ss = new Second(fnew, "welcome");
Second ss1= new Second(fnew,"new");
Second ss2 = new Second(fnew, "programmer");
}
}
13
Implementation of Synchronization
Synchronization can be accomplished by
Using synchronized methods
Using synchronized statement
14
Using synchronized methods (cont..)
synchronize above program, we must
synchronize access to the shared display()
method, making it available to only one
thread at a time
done by using keyword synchronized with
display() method
public synchronized void display (String msg)
15
Using synchronized statement
synchronize access to an object of a class or
only a part of a method to be synchronized,
use synchronized block
capable to make any part of the object and
method synchronized
Syntax
synchronized (object){
//statements to be synchronized
}
16
Using synchronized statement (cont..)
class Second extends Thread{
String msg;
First fobj;
Second (First fp,String str){
fobj = fp;
msg = str;
start();
}
public void run(){
synchronized(fobj){
fobj.display(msg);
}
}
}
17
Comparison between synchronized
methods & statement
using synchronized keyword with a method, it
acquires a lock in the object for the whole
method, means that no other thread can use
any synchronized method until the current
thread, which has invoked it's synchronized
method, has finished its execution
18
Comparison between synchronized
methods & statement (cont..)
synchronized block acquires a lock in the
object only between parentheses after the
synchronized keyword, means that no other
thread can acquire a lock on the locked
object until the synchronized block exits, but
other threads can access the rest of the code
of the method
19
References
https://www.tutorialspoint.com/java/java_multithreading.htm
https://www.javatpoint.com/multithreading-in-java
https://www.geeksforgeeks.org/multithreading-in-java/
http://tutorials.jenkov.com/java-concurrency/index.html
https://docs.oracle.com/javase/tutorial/essential/concurrency
/sync.html
https://www.javatpoint.com/synchronization-in-java
https://www.geeksforgeeks.org/synchronized-in-java/
20