Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University...
Transcript of Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University...
![Page 1: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/1.jpg)
Java Thread: Evaluation
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/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
• Recognize common thread mechanisms
• Appreciate Java thread “happens-before” orderings
• Understand the implementation of the GCD concurrent app
• Know the pros & cons of Javathread programming models
Learning Objectives in this Part of the Lesson
![Page 3: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/3.jpg)
3
Pros & Cons of Java Thread Programming Models
![Page 4: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/4.jpg)
4
Pros & Cons of Java Thread Programming Models • Now that we’ve examined the source code for the GCD concurrent app we’ll
summarize the pros & cons of the various Java thread programming models
![Page 5: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/5.jpg)
5
Pros & Cons of Java Thread Programming Models • Pros with extending Thread public class GCDThread
extends Thread {
...
private MainActivity mActivity;
public GCDThread setActivity
(MainActivity activity) {
mActivity = activity;
return this;
}
private int computeGCD
(int number1, number2) {
...
}
public void run()
{ ... }
...
}
![Page 6: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/6.jpg)
6
Pros & Cons of Java Thread Programming Models • Pros with extending Thread
• It’s straightforward to extendthe Thread super class
public class GCDThread
extends Thread {
...
private MainActivity mActivity;
public GCDThread setActivity
(MainActivity activity) {
mActivity = activity;
return this;
}
private int computeGCD
(int number1, number2) {
...
}
public void run()
{ ... }
...
}
![Page 7: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/7.jpg)
7
Pros & Cons of Java Thread Programming Models • Pros with extending Thread
• It’s straightforward to extendthe Thread super class
• Just override the run() hookmethod!
public class GCDThread
extends Thread {
...
private MainActivity mActivity;
public GCDThread setActivity
(MainActivity activity) {
mActivity = activity;
return this;
}
private int computeGCD
(int number1, number2) {
...
}
public void run()
{ ... }
...
}
![Page 8: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/8.jpg)
8
Pros & Cons of Java Thread Programming Models • Pros with extending Thread
• It’s straightforward to extendthe Thread super class
• All state & methods are consolidated in one place
public class GCDThread
extends Thread {
...
private MainActivity mActivity;
public GCDThread setActivity
(MainActivity activity) {
mActivity = activity;
return this;
}
...
// Main app
Thread thread = new GCDThread()
.setActivity(this)...;
thread.start();
...
![Page 9: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/9.jpg)
9
Pros & Cons of Java Thread Programming Models • Pros with extending Thread
• It’s straightforward to extendthe Thread super class
• All state & methods are consolidated in one place
• Enables central allocation &management of the thread
public class GCDThread
extends Thread {
...
private MainActivity mActivity;
public GCDThread setActivity
(MainActivity activity) {
mActivity = activity;
return this;
}
...
// Main app
Thread thread = new GCDThread()
.setActivity(this)...;
thread.start();
...
![Page 10: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/10.jpg)
10
Pros & Cons of Java Thread Programming Models • Pros with extending Thread
• It’s straightforward to extendthe Thread super class
• All state & methods are consolidated in one place
• Enables central allocation &management of the thread
• This design is useful when the thread must be updated during runtime configuration changes
public class GCDThread
extends Thread {
...
private MainActivity mActivity;
public GCDThread setActivity
(MainActivity activity) {
mActivity = activity;
return this;
}
...
// Main app
Thread thread = new GCDThread()
.setActivity(this)...;
thread.start();
...
![Page 11: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/11.jpg)
11
Pros & Cons of Java Thread Programming Models • Pros with extending Thread
• It’s straightforward to extendthe Thread super class
• All state & methods are consolidated in one place
• Enables central allocation &management of the thread
• This design is useful when the thread must be updated during runtime configuration changes
• e.g., interrupting/restarting a running thread & reading/writing its state
public class GCDThread
extends Thread {
...
private MainActivity mActivity;
public GCDThread setActivity
(MainActivity activity) {
mActivity = activity;
return this;
}
...
// Main app
Thread thread = new GCDThread()
.setActivity(this)...;
thread.start();
...
See the upcoming lessons on “Managing the Java Thread Lifecycle”
![Page 12: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/12.jpg)
12
Pros & Cons of Java Thread Programming Models • Cons with extending Thread public class GCDThread
extends Thread {
...
private int computeGCD
(int number1, number2) {
...
}
public void run() {
...
}
...
}
![Page 13: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/13.jpg)
13
Pros & Cons of Java Thread Programming Models • Cons with extending Thread
• A subclass must extend the Thread superclass
public class GCDThread
extends Thread {
...
private int computeGCD
(int number1, number2) {
...
}
public void run() {
...
}
...
}
![Page 14: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/14.jpg)
14
Pros & Cons of Java Thread Programming Models • Cons with extending Thread
• A subclass must extend the Thread superclass
• This is restrictive since Java only allows one superclass per subclass!
public class GCDThread
extends Thread {
...
private int computeGCD
(int number1, number2) {
...
}
public void run() {
...
}
...
}
See docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
![Page 15: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/15.jpg)
15
Pros & Cons of Java Thread Programming Models • Pros of implementing Runnable public class GCDRunnable
implements Runnable,
implements Serializable,
extends Random {
...
private int computeGCD
(int number1, number2) {
...
}
public void run() {
...
}
...
![Page 16: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/16.jpg)
16
Pros & Cons of Java Thread Programming Models • Pros of implementing Runnable
• A subclass can implement multiple interfaces
See docs.oracle.com/javase/tutorial/java/concepts/interface.html
public class GCDRunnable
implements Runnable,
implements Serializable,
extends Random {
...
private int computeGCD
(int number1, number2) {
...
}
public void run() {
...
}
...
![Page 17: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/17.jpg)
17
Pros & Cons of Java Thread Programming Models • Pros of implementing Runnable
• A subclass can implement multiple interfaces
• Which enables it to extend a different superclass
public class GCDRunnable
implements Runnable,
implements Serializable,
extends Random {
...
private int computeGCD
(int number1, number2) {
...
}
public void run() {
...
}
...
See docs.oracle.com/javase/tutorial/java/concepts/interface.html
![Page 18: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/18.jpg)
18
Pros & Cons of Java Thread Programming Models • Pros of implementing Runnable
• A subclass can implement multiple interfaces
• Runnables are flexible since they can be reused in other contexts
See upcoming lessons on “the Java Executor framework”
public class GCDRunnable
implements Runnable,
... {
...
}
...
GCDRunnable runnableCommand =
new GCDRunnable(...);
ExecutorService executor =
Executors.newFixedThreadPool
(POOL_SIZE);
...
executor.execute
(runnableCommand);
![Page 19: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/19.jpg)
19
Pros & Cons of Java Thread Programming Models • Cons of implementing Runnable public class GCDRunnable
implements Runnable,
... {
...
}
...
GCDRunnable runnableCommand =
new GCDRunnable(...);
Thread thr =
new Thread(runnableCommand);
...
thr.start();
![Page 20: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/20.jpg)
20
Pros & Cons of Java Thread Programming Models • Cons of implementing Runnable
• Yields more “moving parts”
public class GCDRunnable
implements Runnable,
... {
...
}
...
GCDRunnable runnableCommand =
new GCDRunnable(...);
Thread thr =
new Thread(runnableCommand);
...
thr.start();
![Page 21: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/21.jpg)
21
Pros & Cons of Java Thread Programming Models • Cons of implementing Runnable
• Yields more “moving parts”
• e.g., Runnable & Thread are separate entities & must be managed/accessed separately
This decoupling get complicated if a program needs to access the state of a runnable, but only holds a reference to the thread object..
public class GCDRunnable
implements Runnable,
... {
...
}
...
GCDRunnable runnableCommand =
new GCDRunnable(...);
Thread thr =
new Thread(runnableCommand);
...
thr.start();
![Page 22: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/22.jpg)
22
Pros & Cons of Java Thread Programming Models • In practice, Java & Android software often implements Runnable rather than
extending Thread
Thread
Thread(Runnable)
start()
…
Runnable
run()
MyRunnable
run()
…
Thread
run()
start()
…
MyThread
run()
…
![Page 23: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/23.jpg)
23
Pros & Cons of Java Thread Programming Models • In practice, Java & Android software often implements Runnable rather than
extending Thread
• Lambda expressions have become a popular to provide computations tothreads on Java 8-based platforms
See www.drdobbs.com/jvm/lambda-expressions-in-java-8/240166764
new Thread(() ->
System.out.println("hello world"))
.start();
Define a computation that will run in a separate Java thread
Runtimethread stack
![Page 24: Java Thread: Evaluationschmidt/cs891s/2020-PDFs/2.2.7-Thread-pt… · Vanderbilt University Nashville, Tennessee, USA. 2 •Understand how Java threads support concurrency •Learn](https://reader034.fdocuments.us/reader034/viewer/2022042216/5ebe2d43493d1e18af0c208e/html5/thumbnails/24.jpg)
24
End of Java Thread: Evaluation