0% found this document useful (0 votes)
1 views37 pages

Cc02 Java Threads-3

The document provides an overview of Java threads, including how to create them, the concept of execution blocks, and the importance of starting threads correctly. It discusses race conditions, shared state, and the differences between passing objects and primitive types to threads. The document also emphasizes the need for synchronization to avoid race hazards and includes references for further reading.

Uploaded by

Husain Presswala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views37 pages

Cc02 Java Threads-3

The document provides an overview of Java threads, including how to create them, the concept of execution blocks, and the importance of starting threads correctly. It discusses race conditions, shared state, and the differences between passing objects and primitive types to threads. The document also emphasizes the need for synchronization to avoid race hazards and includes references for further reading.

Uploaded by

Husain Presswala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Threads

Operating Systems & Concurrency

F29OC
Mike Chantler
You will Learn
• Java Threads
– How to create them
– What execution blocks are
– How to start threads running
• Revisit Ex01
– Race conditions
– Shared state
• Gitlab examples and readings

26-Feb-25 1
You already use one
Thread
When you write Java programs you use, by
default, the “main” thread

26-Feb-25 2
Creating Extra Threads

main thread
CPU
myThread
time

26-Feb-25 3
The ‘Execution Block’
time

main thread
CPU
myThread

26-Feb-25 4
Threads vs Execution Blocks
• Thread instance
 virtual CPU

• Execution block
= code to run on thread instance

26-Feb-25 5
Two ways to specify the
‘Execution Block’
time

main thread
CPU
myThread

1. Subclass Thread (specifying its run method)


2. Create a Runnable (execution block) and pass as
argument to a Thread instance
Both approaches allow you to specify the code of the
thread’s run method (the execution block)

26-Feb-25 7
1. Create ‘Runnable’

8
2. Subclassing Thread
(and specifying it’s run method)

26-Feb-25 11
Starting the Thread

26-Feb-25 12
Subclass or Runnable?
• For F29OC it does not really matter
• Subclassing has the advantage that you
cannot share instances of threads’ execution
block between threads (!)

26-Feb-25 13
Common Pitfall!!

Calling run() Instead of start()!!!!


From <[Link]

26-Feb-25 14
REVISITING: EX01
MULTI-THREADED EXPERIMENT
Ex01 - Multi-thread Processing Demo
One thread 20 threads 20 threads sync

x20 each each

6 6 6
x10 x10 x10

17
Reminder ‘20 Thread’ Version
Two Runs:

26-Feb-25 18
‘20 Thread’ version

26-Feb-25 20
‘20 Thread’ version

21
‘20 Thread’ version
Thread
0

Thread
1
Thread
2

Thread
19

26-Feb-25 22
‘20 Thread’ version
Thread
0

Thread
1
Thread
2

Thread How many instances and where:


19 • main stack,
• 20xThreads’ stacks,
26-Feb-25 • or heap? 23
Passing the accumulator
Thread
0

Thread
1
Thread
2

How many instances and where:


• main stack,
• 20xThreads’ stacks,
• or heap?

Thread
19

26-Feb-25 24
‘int’ version

Thread
0
Thread
1
Thread
2

How many instances and where:


Thread
• main stack,
19
• 20xThreads’ stacks,
• or heap? 25
Passing the int accumulator
Thread
0

Thread
1
Thread
2

How many instances and where:


• main stack,
• 20xThreads’ stacks,
• or heap?

Thread
19

26-Feb-25 26
Thread
0
Thread
1
Thread
2

Thread
19
27
So what’s happening?
int accumulator version:
Thread
0
int accumulator Thread
object passed 1
by value to
Thread
each of
2
20 threads • Local copy of accumulator value stored in each
thread’s stack
• All 20 threads increment the int accumulator
concurrently
• No “race” condition

“Shared state” of 20 threads


Thread = empty set
19

28
So what’s happening?
int accumulator version:
Thread
0
int accumulator Thread
object passed 1
by value to
Thread
each of
2
20 threads • Local copy of accumulator value stored in each
thread’s stack
• All 20 threads increment the int accumulator
concurrently
• No “race” condition

“Shared state” of 20 threads


Thread = empty set
19

29
So what’s happening?
int accumulator version:
Thread
0
int accumulator Thread
object passed 1
by value to
Thread
each of
2
20 threads

Thread
19

30
• Local cop
int accumulator version
Example run:
Thread
0
int accumulator Thread
object passed 1
by value to
Thread
each of
2
20 threads

Thread
19

31
So what’s happening?
Accumulator object version:
Thread
0
accumulator Thread
object passed 1
by reference to
Thread
each of
2
20 threads • One accumulator object stored on heap
• References to accumulator
passed to the 20 threads
• All 20 threads try and increment the
accumulator sum concurrently
• Gives “race” condition

Thread “Shared state” of 20 threads


19 = accumulator object

32
So what’s happening?
Accumulator object version:
Thread
0
accumulator Thread
object passed 1
by reference to
Thread
each of
2
20 threads

Thread
19

33
The Race Problem
time

read a a++ store a


CPU
read a a++ store a

26-Feb-25 34
‘20 Thread’ Version
Two Runs:

26-Feb-25 36
So what is “Shared State”?
• Refers to a variables or objects that can:
– be modified by at least one thread, and
– read by at least one other thread
• Can lead to race conditions
– if not properly synchronized with mechanisms like
locks or mutexes.

26-Feb-25 37
Takeaway Messages
• The run method is a thread’s ‘single thread of control’
– Program by overriding run method
– Call thread’s .start() method to tell scheduler that the thread is ready to run.
• Objects and primitive types can be passed to threads via constructor arguments
– Objects: passed by reference
• Object stored on heap
• Can be written to, and read by threads
• Hence part of shared state
– Primitive types: passed by value
• Primitive types (eg int variables)
• Stored on thread’s stack
• Not part of shared state
– Primitive types with object wrappers
• e.g. Integer class
• Stored on thread’s stack
• Not part of shared state
• Important:
– Know what the shared state of your threads is
– Protect these state variables with mutex otherwise you may have a race hazard!!!
– (think of the unsafe 20 thread Ex01 example)

26-Feb-25 39
GitLab

26-Feb-25 41
Readings
• Jenkov
• Oracle
• Volgella

26-Feb-25 42
Good Tutorial from Jenkov
Creating and Starting Java
Threads
•Java Threads Video Tutorial
•Creating and Starting Threads
•Thread Subclass
•Runnable Interface Implementation
o Java Class Implements Runnable
o Anonymous Implementation of Runnable
o Java Lambda Implementation of Runnable
o Starting a Thread With a Runnable
•Subclass or Runnable?
•Common Pitfall: Calling run() Instead of start()
•Thread Names
•[Link]()
•Java Thread Example
•Pause a Thread
•Stop a Thread

26-Feb-25 From <[Link] 43


End of this slide set

26-Feb-25 46

You might also like