overview of java threads (part 2)schmidt/cs891s/2019-pdfs/l1...running java threads operating system...
TRANSCRIPT
![Page 1: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/1.jpg)
Overview of Java Threads
(Part 2)
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/2.jpg)
2
• Understand how Java threads support concurrency
• Learn how our case study app works
• Know alternative ways of giving code to a thread
• Learn how to pass parameters to a Java thread
• Know how to run a Java thread
Learning Objectives in this Part of the Lesson
: My
Component
start()
run()
new()
: MyThread
onCreate()
![Page 3: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/3.jpg)
3
• Understand how Java threads support concurrency
• Learn how our case study app works
• Know alternative ways of giving code to a thread
• Learn how to pass parameters to a Java thread
• Know how to run a Java thread
• Recognize common thread methods
Learning Objectives in this Part of the Lesson
![Page 4: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/4.jpg)
4
• Understand how Java threads support concurrency
• Learn how our case study app works
• Know alternative ways of giving code to a thread
• Learn how to pass parameters to a Java thread
• Know how to run a Java thread
• Recognize common thread methods
• Appreciate Java thread “happens-before” orderings
Learning Objectives in this Part of the Lesson
![Page 5: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/5.jpg)
5
RunningJava Threads
![Page 6: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/6.jpg)
6
• There are multiple layers involved in creating & starting a thread
Running Java Threads
Operating System Kernel
System Libraries
Java Execution Environment (e.g., JVM, ART, etc)
Threading & Synchronization Packages
: My
Component
start()
run()
new()
: MyThread
onCreate()
See Part 2 of the upcoming lesson on “Managing the Java Thread Lifecycle”
![Page 7: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/7.jpg)
7
• There are multiple layers involved in creating & starting a thread
• Creating a new thread object doesn’t allocate a run-time call stack of activation records
: My
Component
new()
Running Java Threads
: MyThread
onCreate()
See en.wikipedia.org/wiki/Call_stack
![Page 8: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/8.jpg)
8
• There are multiple layers involved in creating & starting a thread
• Creating a new thread object doesn’t allocate a run-time call stack of activation records
• The runtime stack & other thread resources are only allocated after the start() method is called
: My
Component
start()
run()
new()
Running Java Threads
: MyThread
onCreate()
![Page 9: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/9.jpg)
9
• There are multiple layers involved in creating & starting a thread
• Creating a new thread object doesn’t allocate a run-time call stack of activation records
• The runtime stack & other thread resources are only allocated after the start() method is called
• The Java execution environment calls a thread’s run() hook method after start() creates its resources
: My
Component
start()
run()
new()
Running Java Threads
: MyThread
onCreate()
See wiki.c2.com/?HookMethod
![Page 10: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/10.jpg)
10
• There are multiple layers involved in creating & starting a thread
• Creating a new thread object doesn’t allocate a run-time call stack of activation records
• The runtime stack & other thread resources are only allocated after the start() method is called
• The Java execution environment calls a thread’s run() hook method after start() creates its resources
• Each thread can run concurrently &block independently
: My
Component
start()
run()
new()
Running Java Threads
: MyThread
onCreate()
![Page 11: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/11.jpg)
11
• Any code can generally run in a thread : My
Component
start()
run()
new()
Running Java Threads
: MyThread
onCreate()
public void run(){
// code to run goes here
}
![Page 12: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/12.jpg)
12
• Any code can generally run in a thread
• However, windowing toolkits often restrict which thread can access GUI components
: My
Component
start()
run()
new()
Running Java Threads
: MyThread
onCreate()
![Page 13: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/13.jpg)
13
• Any code can generally run in a thread
• However, windowing toolkits often restrict which thread can access GUI components
• e.g., only the Android UI thread can access GUI components
: My
Component
start()
run()
new()
Running Java Threads
: MyThread
See developer.android.com/training/multiple-threads/communicate-ui.html
onCreate()
![Page 14: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/14.jpg)
14
• A thread can live as long as its run() hook method hasn’t returned
Running Java Threads
: My
Component
start()
new()
run()
: MyThread
onCreate()
![Page 15: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/15.jpg)
15
• A thread can live as long as its run() hook method hasn’t returned
• The underlying thread scheduler can suspend & resume a thread many times during its lifecycle
Running Java Threads
: My
Component
onCreate()
start()
run()
new()
: MyThread
See en.wikipedia.org/wiki/Scheduling_(computing)
![Page 16: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/16.jpg)
16
• A thread can live as long as its run() hook method hasn’t returned
• The underlying thread scheduler can suspend & resume a thread many times during its lifecycle
• Scheduler operations are largely invisible to user code, as long as synchronization is performed properly..
Running Java Threads
: My
Component
start()
run()
new()
: MyThread
onCreate()
![Page 17: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/17.jpg)
17
• For a thread to execute “forever,” its run() hook method needs an infinite loop
Running Java Threads
: My
Component
start()
run()
new()
: MyThread
public void run(){
while (true) { ... }
}
onCreate()
![Page 18: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/18.jpg)
18
Running Java Threads• The thread is dead after run() returns : My
Component
start()
run()
new()
: MyThread
onCreate()
![Page 19: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/19.jpg)
19
Running Java Threads• The thread is dead after run() returns
• A thread can end normally: My
Component
start()
run()
new()
: MyThread
onCreate()
public void run(){
while (true) {
...
return;
}
}
![Page 20: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/20.jpg)
20
Running Java Threads• The thread is dead after run() returns
• A thread can end normally
• Or an uncaught exception canbe thrown
: My
Component
start()
run()
new()
: MyThread
onCreate()
public void run(){
while (true) {
...
throw new
SomeException();
}
}
See www.javamex.com/tutorials/exceptions/exceptions_uncaught_handler.shtml
![Page 21: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/21.jpg)
21
Running Java Threads• The join() method allows one thread to
wait for another thread to complete: My
Component
start()
run()
new()
join()
: MyThread
onCreate()
![Page 22: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/22.jpg)
22
Running Java Threads• The join() method allows one thread to
wait for another thread to complete: My
Component
start()
run()
new()
join()
: MyThread
See upcoming lessons on “Java Barrier Synchronizers”
Simple form of “barrier synchronization”
onCreate()
![Page 23: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/23.jpg)
23
: My
Component
Running Java Threads• The join() method allows one thread to
wait for another thread to complete
• Or a thread can simply evaporate!onCreate()
![Page 24: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/24.jpg)
24
: My
Component
Running Java Threads• The join() method allows one thread to
wait for another thread to complete
• Or a thread can simply evaporate!
• The Java execution environmentrecycles thread resources
onCreate()
![Page 25: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/25.jpg)
25
: My
Component
Running Java Threads• The join() method allows one thread to
wait for another thread to complete
• Or a thread can simply evaporate!
• The Java execution environmentrecycles thread resources
• e.g., runtime stack of activation records, thread-specific storage, etc.
onCreate()
![Page 26: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/26.jpg)
26
Some Common Java Thread Methods
![Page 27: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/27.jpg)
27
Some Common Java Thread Methods
See docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
• There are a number of commonly used methods in the Java Thread class
![Page 28: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/28.jpg)
28
Some Common Java Thread Methods• There are a number of commonly used
methods in the Java Thread class, e.g.,
• void setDaemon()
• Marks thread as a “daemon”
See javarevisited.blogspot.com/2012/03/what-is-daemon-thread-in-java-and.html
![Page 29: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/29.jpg)
29
Some Common Java Thread Methods• There are a number of commonly used
methods in the Java Thread class, e.g.,
• void setDaemon()
• void start()
• Allocates thread resources & initiates thread execution by calling the run() hook method
![Page 30: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/30.jpg)
30
Some Common Java Thread Methods• There are a number of commonly used
methods in the Java Thread class, e.g.,
• void setDaemon()
• void start()
• void run()
• Hook method where user code is supplied
![Page 31: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/31.jpg)
31
Some Common Java Thread Methods• There are a number of commonly used
methods in the Java Thread class, e.g.,
• void setDaemon()
• void start()
• void run()
• void join()
• Waits for a thread to finish
A simple form of “barrier synchronization”
![Page 32: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/32.jpg)
32
Some Common Java Thread Methods• There are a number of commonly used
methods in the Java Thread class, e.g.,
• void setDaemon()
• void start()
• void run()
• void join()
• void sleep(long time)
• Sleeps for given time in ms
![Page 33: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/33.jpg)
33
Some Common Java Thread Methods• There are a number of commonly used
methods in the Java Thread class, e.g.,
• void setDaemon()
• void start()
• void run()
• void join()
• void sleep(long time)
• Thread currentThread()
• Object for current Thread
![Page 34: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/34.jpg)
34
Some Common Java Thread Methods• There are a number of commonly used
methods in the Java Thread class, e.g.,
• void setDaemon()
• void start()
• void run()
• void join()
• void sleep(long time)
• Thread currentThread()
• void interrupt()
• Post an interrupt request to a Thread
See part 3 of upcoming lesson on “Managing the Java Thread Lifecycle”
![Page 35: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/35.jpg)
35
Some Common Java Thread Methods• There are a number of commonly used
methods in the Java Thread class, e.g.,
• void setDaemon()
• void start()
• void run()
• void join()
• void sleep(long time)
• Thread currentThread()
• void interrupt()
• boolean isInterrupted()
• Tests whether a thread has been interrupted
isInterrupted() can be called multiple times w/out affecting the interrupted status
![Page 36: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/36.jpg)
36
Some Common Java Thread Methods• There are a number of commonly used
methods in the Java Thread class, e.g.,
• void setDaemon()
• void start()
• void run()
• void join()
• void sleep(long time)
• Thread currentThread()
• void interrupt()
• boolean isInterrupted()
• boolean interrupted()
• Tests whether current thread has been interrupted
interrupted() clears the interrupted status the first time it’s called
![Page 37: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/37.jpg)
37
Some Common Java Thread Methods• There are a number of commonly used
methods in the Java Thread class, e.g.,
• void setDaemon()
• void start()
• void run()
• void join()
• void sleep(long time)
• Thread currentThread()
• void interrupt()
• boolean isInterrupted()
• boolean interrupted()
• void setPriority(int newPriority)
& int getPriority()
• Set & get the priority of a Thread
![Page 38: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/38.jpg)
38
Java Thread “Happens-Before” Orderings
![Page 39: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/39.jpg)
39
Java Thread “Happens-Before” Orderings• Java Threads methods establish “happens-before”
orderings
See en.wikipedia.org/wiki/Happened-before
![Page 40: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/40.jpg)
40
• Java Threads methods establish “happens-before” orderings
• Ensure that if one event “happens before” another event, the result must reflect that, even if those events are actually executed out of order
Java Thread “Happens-Before” Orderings
See en.wikipedia.org/wiki/Happened-before
![Page 41: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/41.jpg)
41
• Java Threads methods establish “happens-before” orderings
• Ensure that if one event “happens before” another event, the result must reflect that, even if those events are actually executed out of order
• e.g., to optimize program flow & concurrency
Java Thread “Happens-Before” Orderings
![Page 42: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/42.jpg)
42
• Java Threads methods establish “happens-before” orderings
• Ensure that if one event “happens before” another event, the result must reflect that, even if those events are actually executed out of order
• In general, a happens-before relationship guarantees that memory written to by statement A is visible to statement B
Java Thread “Happens-Before” Orderings
i.e., statement A completes its write to “ready” before statement B starts its read
![Page 43: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/43.jpg)
43
• Examples of “happens-before” orderings in Java
Java Thread “Happens-Before” Orderings
See en.wikipedia.org/wiki/Java_memory_model
![Page 44: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/44.jpg)
44
• Examples of “happens-before” orderings in Java
• Starting a thread “happens-before” the run() hook method of the thread is called
Thread t1 =
new Thread(() ->
System.out.println
("hello world"))
.start();
Java Thread “Happens-Before” Orderings
![Page 45: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/45.jpg)
45
• Examples of “happens-before” orderings in Java
• Starting a thread “happens-before” the run() hook method of the thread is called, e.g.
Thread t1 =
new Thread(() ->
System.out.println
("hello world"))
.start();
This lambda plays the role of the run()
hook method!
Java Thread “Happens-Before” Orderings
![Page 46: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/46.jpg)
46
• Examples of “happens-before” orderings in Java
• Starting a thread “happens-before” the run() hook method of the thread is called, e.g.
Thread t1 =
new Thread(() ->
System.out.println
("hello world"))
.start();
A thread’s state is consistent & visible before run() starts
Java Thread “Happens-Before” Orderings
![Page 47: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/47.jpg)
47
• Examples of “happens-before” orderings in Java
• Starting a thread “happens-before” the run() hook method of the thread is called
• Methods in java.util.concurrent package classes also establish “happen-before” orderings
Java Thread “Happens-Before” Orderings
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html
![Page 48: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/48.jpg)
48
• Examples of “happens-before” orderings in Java
• Starting a thread “happens-before” the run() hook method of the thread is called
• Methods in java.util.concurrent package classes also establish “happen-before” orderings, e.g.
Placing an object into a concurrent collection happens-before the access or
removal of the element from the collection
// Thread t1
ConcurrentMap concurrentMap =
new ConcurrentHashMap();
concurrentMap.put("key", "value");
// Thread t2
Object value = concurrentMap.get("key");
Java Thread “Happens-Before” Orderings
![Page 49: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/49.jpg)
49
• Examples of “happens-before” orderings in Java
• Starting a thread “happens-before” the run() hook method of the thread is called
• Methods in java.util.concurrent package classes also establish “happen-before” orderings
• The termination of a thread “happens-before” a join() with the terminated thread
Thread t1 =
new Thread(() ->
System.out.println
("hello world"))
.start();
t1.join();
Java Thread “Happens-Before” Orderings
![Page 50: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/50.jpg)
50
• Examples of “happens-before” orderings in Java
• Starting a thread “happens-before” the run() hook method of the thread is called
• Methods in java.util.concurrent package classes also establish “happen-before” orderings
• The termination of a thread “happens-before” a join() with the terminated thread, e.g.
Thread t1 =
new Thread(() ->
System.out.println
("hello world"))
.start();
t1.join();
Java Thread “Happens-Before” Orderings
This thread terminates after its lambda expression runnable completes
![Page 51: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/51.jpg)
51
• Examples of “happens-before” orderings in Java
• Starting a thread “happens-before” the run() hook method of the thread is called
• Methods in java.util.concurrent package classes also establish “happen-before” orderings
• The termination of a thread “happens-before” a join() with the terminated thread, e.g.
A thread waiting on a (non-timed) join() only resumes after the target thread terminates
Java Thread “Happens-Before” Orderings
Thread t1 =
new Thread(() ->
System.out.println
("hello world"))
.start();
t1.join();
![Page 52: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/52.jpg)
52
• The implementations of theseJava thread & library classes are responsible for ensuring that these “happens-before” orderings are preserved
Java Thread “Happens-Before” Orderings
You don’t need to understand all the nitty-gritty details of Java’s memory model – you just need to understand how to use synchronizers properly!
![Page 53: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/53.jpg)
53
End of Overview of Java Threads (Part 2)
![Page 54: Overview of Java Threads (Part 2)schmidt/cs891s/2019-PDFs/L1...Running Java Threads Operating System Kernel System Libraries Java Execution Environment (e.g., JVM, ART, etc) Threading](https://reader036.vdocument.in/reader036/viewer/2022081613/5fb38eba00a7b823ce39b570/html5/thumbnails/54.jpg)
54
1. Which of the following are correct statements about the key differences between the Java Thread start() & run() methods?
a. The start() method sets the priority of the thread & the run() method allocates the thread’s resources
b. The start() method allocates the thread’s resources & dispatches the join() method, which implements user-supplied code
c. The start() method allocates the thread’s resources & dispatches the run() method, which implements user-supplied code
d. The start() method allocates the thread’s resources & dispatches the run() method, which implements barrier synchronization
Discussion Questions