Based on Chapter 2 of [Hai19]
(Usage hints for this presentation)
Computer Structures and Operating Systems 2023
Dr. Jens Lechtenbörger (License Information)
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.
main()
(for
main thread) or run()
(for other threads) end (if
at all)Concurrency is more general term than parallelism
Parallel threads (on multiple CPU cores)
Interleaved threads (taking turns on single CPU core)
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."); }}
This task is available for self-study in Learnweb.
jdb
, e.g., the
OpenJDK tools.
(The notes on this slide contain
sample commands for jdb
.)
“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
“Figure 2.5 of [Hai19]” by Max Hailperin under CC BY-SA 3.0; converted from GitHub
yield
In the following
switchFromTo()
on next slide
yield()
may
really be an OS system call
Interleaved execution of threads. Based on Figure 2.7 of book by Max Hailperin, CC BY-SA 3.0.
Figure by Jens Lechtenbörger under CC BY-SA 4.0; from GitLab
switchFromTo()
are really (pointers to) TCBsswitchFromTo()
is cooperative
Single thread (left-hand side of Figure 2.5; thought experiment, not for use)
“Figure 2.5 of [Hai19]” by Max Hailperin under CC BY-SA 3.0; converted from GitHub
This document is part of an Open Educational Resource (OER) course on Operating Systems. Source code and source files are available on GitLab under free licenses.
Except where otherwise noted, the work “OS03: Threads”, © 2017-2023 Jens Lechtenbörger, is published under the Creative Commons license CC BY-SA 4.0.
In particular, trademark rights are not licensed under this license. Thus, rights concerning third party logos (e.g., on the title slide) and other (trade-) marks (e.g., “Creative Commons” itself) remain with their respective holders.