java thread: how threads run - vanderbilt universityschmidt/cs891s/2020-pdfs/2... · a java thread...

25
Java Thread: How Threads Run Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Upload: others

Post on 24-Jul-2020

50 views

Category:

Documents


0 download

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

25

End of Java Thread: How Threads Run