(Usage hints for this presentation)
IT Systems, Summer Term 2024
Dr. Jens Lechtenbörger (License Information)
(Based on Chapter 2 of (Hailperin 2019))
What are interfaces and classes in Java, what is “this”?
If you are not certain, consult a textbook; these self-check questions and preceding tutorials may help:
Stack.Push(o)
: place object o
on top of Stack
Stack.Pop()
: remove object from top of Stack
and return it
What's the stack?
Figure © 2016 Julia Evans, all rights reserved; from julia's drawings. Displayed here with personal permission.
What is a thread?
Threads!
Figure © 2016 Julia Evans, all rights reserved; from julia's drawings. Displayed here with personal permission.
Concurrency is more general term than parallelism
Parallel threads (on multiple CPU cores)
Interleaved threads (taking turns on single CPU core)
Program vs thread
OS schedules execution of threads
Programmer determines how many threads are created
Different OSes and different languages provide different APIs to manage threads
Thread creation
main()
(for
main thread) or run()
(for other threads) end (if
at all)I/O bound vs CPU bound
I/O bound
Resource utilization
Responsiveness
More modular design
“Interleaved execution example” by Jens Lechtenbörger under CC BY-SA 4.0; SVG image refers to converted and cut parts of Figure 2.6 of a book by Max Hailperin under CC BY-SA 3.0. From GitLab
run()
methodRunnable
instancestart()
method on Thread
instanceThread
(Thread implements Runnable
)
run()
overwrittenCallable<V>
, Future<V>
and service methods of Executor
public class Simpler2Threads { // Based on Fig. 2.3 of [Hai17]
// "Simplified" by removing anonymous class.
public static void main(String args[]){
Thread childThread = new Thread(new MyThread());
childThread.start();
sleep(5000);
System.out.println("Parent is done sleeping 5 seconds.");}
static void sleep(int milliseconds){
// Sleep milliseconds (blocked/removed from CPU).
try{ Thread.sleep(milliseconds); } catch(InterruptedException e){
// ignore this exception; it won't happen anyhow
}}}
class MyThread implements Runnable {
public void run(){
Simpler2Threads.sleep(3000);
System.out.println("Child is done sleeping 3 seconds.");
}}
Aspect of this task are available for self-study in Learnweb.
jdb
with
OpenJDK tools:
Web server “talks” HTTP with browsers
“Figure 2.5 of cite:Hai19” by Max Hailperin under CC BY-SA 3.0; converted from GitHub
Source files are available on GitLab (check out embedded submodules) under free licenses. Icons of custom controls are by @fontawesome, released under CC BY 4.0.
Except where otherwise noted, the work “Threads”, © 2017-2024 Jens Lechtenbörger, is published under the Creative Commons license CC BY-SA 4.0.