Prévia do material em texto
THREAD
PROGRAMMING IN
JAVA
Prof. Dr. Everton Cavalcante
https://www.docente.ufrn.br/everton.cavalcante
https://www.docente.ufrn.br/everton.cavalcante
IDEAL INTERFACE FOR THREAD PROGRAMMING
• Creation
• Termination
• Wait
• Identifier retrieval
• Identifier comparison
• Access/modification of attributes
2
GOALS
To introduce concurrent programming using
threads in the Java programming language
3
THREADS IN JAVA
• Every Java program has at least one thread
o main(String) – main method
o It is terminated when all statements within the main method and other
ones called by it are executed
• Java includes multithreading primitives as part of the language
o Portability
• Other threads can be created besides the main thread
o Write the code to be executed by the thread
o Write the code to create and run the thread
4
THE LIFE CYCLE OF THREADS IN JAVA
new
running
wait time wait terminated
program starts
the thread
ti
m
e
e
xp
ir
es
5
CREATING THREADS (1/3)
public class
extends java.lang.Thread
implements java.lang.Runnable
6
CREATING THREADS (2/3)
Extending the java.lang.Thread class
• Instantiation of an object of the class derived from Thread
• Overriding of the run method inherited from the Thread superclass
o Coding the statements to be executed
• Invocation of the start method inherited from the Thread superclass to run
the thread
o This method must not be overridden by the derived class
7
CREATING THREADS (3/3)
Implementing the java.lang.Runnable interface
• Mandatory implementation of the run method defined by Runnable
o Coding the statements to be executed
o Coding a class that implements the Runnable interface promotes separation between the
main program and the task to be executed
• Instantiation of an object of the derived class
• Instantiation of an object of the Thread class by providing the created
Runnable object as a parameter to its constructor
• Invocation of the start method to run the thread
8
IMPORTANT METHODS FOR JAVA
MULTITHREADING (1/3)
Method Description
sleep Make the thread to wait
join Make the thread to wait the termination of another thread
currentThread Return a reference to the running thread
getId Return the thread’s unique identifier
getName Return the name of the thread
isAlive Check if the thread is running
getPriority Return the thread’s priority level
setPriority Modify the thread’s priority level
getState Return the thread state
9
IMPORTANT METHODS FOR JAVA
MULTITHREADING
.sleep
t1
t2
running state wait state
t2.start()
t1.sleep()
t1.run()
t2.run()
t1 resumes executiont1.start()
10
IMPORTANT METHODS FOR JAVA
MULTITHREADING (3/3)
For the sake of consistency, methods that abruptly interrupt the
execution of a thread have been deprecated since JDK 1.2
11
THREAD PRIORITY
Java allows assigning individual priorities to threads towards
driving the scheduler to decide which threads should run
• MAX_PRIORITY
• MIN_PRIORITY
• NORM_PRIORITY
Integer constants defined in
the java.lang.Thread class
12
CHECK IT OUT
Code on GitHub
13
https://github.com/ufrn-concprog/java-threads-examples/
SOME USEFUL TIPS
14
SOME USEFUL TIPS (1/9)
Thread Magic Tricks: 5 Things You Never Knew
You Can Do with Java Threads
15
https://www.harness.io/blog/5-things-you-can-do-with-java-threads
https://www.harness.io/blog/5-things-you-can-do-with-java-threads
SOME USEFUL TIPS (2/9)
1. Naming threads eases debugging
…through parameterized constructor
public class Worker extends Thread {
public Worker(String name) {
super(name);
}
@Override
public void run() {
// Thread's tasks
}
}
16
SOME USEFUL TIPS (3/9)
1. Naming threads eases debugging
…through a setter method
public class Worker extends Thread {
protected String name;
public void setThreadName(String name) {
this.name = name;
}
@Override
public void run() {
// Thread's tasks
}
}
17
SOME USEFUL TIPS (4/9)
2. To wisely use priorities
• Despite Java programs being portable, priorities are handled in distinct ways
according to the operating system
o Each Java thread opens a new native thread on the operating system level, and the Java
priorities set are translated to native priorities in a different way for each platform
o Priorities can be viewed as recommendations to the operating system’s scheduler
o The -XX:+UseThreadPriorities flag must be included when running the program to
consider the priorities
• Using thread priorities in an indiscriminate way may lead them to a
starvation condition
18
SOME USEFUL TIPS (5/9)
3. To use threads to execute tasks in background
• Threads as daemons can execute non-critical tasks in background
• Java allows checking and specifying if a thread is a daemon
o isDaemon and setDaemon methods
• As threads inherit the status of their respective parent threads,
a daemon thread can create other threads as daemons
19
SOME USEFUL TIPS (6/9)
4. To have fine-grained control of the execution of threads over the
processor (a.k.a. processor affinity)
• Processor affinity allows to specify over which processor a given thread will
be executed
o This is defined by the operating system’s scheduler in conjunction with the Java Virtual
Machine (JVM), considering thread priorities
• An interesting case when a thread can use cache and hence have the
required data in a faster way
20
SOME USEFUL TIPS (7/9)
4. To have fine-grained control of the execution of threads over the
processor (a.k.a. processor affinity)
• Linux: command taskset –c
• Java does not provide processor affinity mechanisms
o Thread Affinity is a third-party library that allows binding a thread to a given core
21
https://github.com/OpenHFT/Java-Thread-Affinity
SOME USEFUL TIPS (8/9)
5. To use the Java SE Monitoring and Management facilities to
monitor the execution of threads, either programmatically via
the java.lang.management package or using the JConsole tool
• Number of classes loaded and threads running
• Thread state, statistics, and stack trace
• Memory and CPU usage
• Deadlock detection
22
https://docs.oracle.com/en/java/javase/23/management/overview-java-se-monitoring-and-management.html
https://docs.oracle.com/en/java/javase/23/docs/api/java.management/java/lang/management/package-summary.html
https://docs.oracle.com/en/java/javase/23/management/using-jconsole.html
SOME USEFUL TIPS (9/9)
Using the JConsole tool
23
https://docs.oracle.com/en/java/javase/23/management/using-jconsole.html
THREAD
PROGRAMMING IN
JAVA
Prof. Dr. Everton Cavalcante
https://www.docente.ufrn.br/everton.cavalcante
https://www.docente.ufrn.br/everton.cavalcante
Slide 1: THREAD PROGRAMMING IN JAVA
Slide 2: Ideal interface for thread programming
Slide 3: GOALS
Slide 4: THREADS IN JAVA
Slide 5: THE LIFE CYCLE OF THREADS IN JAVA
Slide 6: CREATING THREADS (1/3)
Slide 7: CREATING THREADS (2/3)
Slide 8: CREATING THREADS (3/3)
Slide 9: Important methods for Java multithreading (1/3)
Slide 10: Important methods for Java multithreading
Slide 11: Important methods for Java multithreading (3/3)
Slide 12: THREAD PRIORITY
Slide 13: CHECK IT OUT
Slide 14: SOME USEFUL TIPS
Slide 15: SOME USEFUL TIPS (1/9)
Slide 16: SOME USEFUL TIPS (2/9)
Slide 17: SOME USEFUL TIPS (3/9)
Slide 18: SOME USEFUL TIPS (4/9)
Slide 19: SOME USEFUL TIPS (5/9)
Slide 20: SOME USEFUL TIPS (6/9)
Slide 21: SOME USEFUL TIPS (7/9)
Slide 22: SOME USEFUL TIPS (8/9)
Slide 23: SOME USEFUL TIPS (9/9)
Slide 24: THREAD PROGRAMMING IN JAVA