java thread: how threads run - vanderbilt universityschmidt/cs891s/2020-pdfs/2... · a java thread...
TRANSCRIPT
![Page 1: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/1.jpg)
Java Thread:
How Threads Run
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/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: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/3.jpg)
3
RunningJava Threads
![Page 4: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/4.jpg)
4
• 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 the upcoming lessons on “Managing the Java Thread Lifecycle”
![Page 5: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/5.jpg)
5
• 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 6: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/6.jpg)
6
• 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 7: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/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
• 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()
The start() method can only be called once per thread object
![Page 8: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/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
• 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 9: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/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
• Each thread can run concurrently &block independently
: My
Component
start()
run()
new()
Running Java Threads
: MyThread
onCreate()
![Page 10: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/10.jpg)
10
• 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 11: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/11.jpg)
11
• 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 12: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/12.jpg)
12
• 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 13: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/13.jpg)
13
• 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 14: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/14.jpg)
14
• 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 15: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/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
• 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 16: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/16.jpg)
16
• 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 17: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/17.jpg)
17
Running Java Threads• The thread is dead after run() returns : My
Component
start()
run()
new()
: MyThread
onCreate()
![Page 18: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/18.jpg)
18
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) {
...
if (someCondition())
return;
}
}
![Page 19: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/19.jpg)
19
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) {
...
if (someError())
throw new
SomeException();
}
}
See www.javamex.com/tutorials/exceptions/exceptions_uncaught_handler.shtml
![Page 20: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/20.jpg)
20
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 21: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/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
See upcoming lessons on “Java Barrier Synchronizers”
Simple form of “barrier synchronization”
onCreate()
![Page 22: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/22.jpg)
22
: 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 23: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/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!
• The Java execution environmentrecycles thread resources
onCreate()
![Page 24: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/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
• e.g., runtime stack of activation records, thread-specific storage, etc.
onCreate()
![Page 25: Java Thread: How Threads Run - Vanderbilt Universityschmidt/cs891s/2020-PDFs/2... · a Java thread •Know how to run a Java thread Learning Objectives in this Part of the Lesson:](https://reader034.vdocuments.site/reader034/viewer/2022042612/5f6bc252fbb93702072f820a/html5/thumbnails/25.jpg)
25
End of Java Thread: How Threads Run