371x Filetype PDF File size 0.26 MB Source: www.cs.auckland.ac.nz
Application-level concurrency: Threads,
a first introduction
Housekeeping
Assignment 4
• Available from Friday May 20. Submission due May 27 4pm
• Looking into multi-threading performance issues. A small amount of cod-
ing.
Mydetails:
Lecturer Ralf Haeusler
Email rhae001@aucklanduni.ac.nz (include “COMPSCI230” in the subject)
Hours Tuesdays (May 16,23,30) after the lecture without appointment or prior
to lecturer with appointment.
Background Reading
The Java Tutorials (online):
• Concurrency: docs.oracle.com/javase/tutorial/essential/concurrency
• Concurrency in Swing: docs.oracle.com/javase/tutorial/uiswing/concurrency
Please read/work through both of these!
Books:
• Brian Goetz. Java concurrency in practice. Addison-Wesley (2008)
• Robert C. Martin. Clean code (Chapter 13). Prentice Hall (2009)
• Joshua Bloch. Effective Java. Addison-Wesley (2008)
For those interested in the details!
Problems addressed by app-level concurrency
1. Designing efficient applications
• E.g. using all available processing power on multi-core machines.
• Example: 3D rendering
2. Designing responsive applications
• Need applications that remain responsive even when a GUI event
handlerstartsalongcomputation(oranetwork/databaseoperation).
1
3. Designing programs that must simultaneously do several distinct logical
tasks
• Browsers must download many different page components in parallel
• Servers must service multiple clients simultaneously
Concurrency with Threads
In general, we consider:
• Concurrency between processes (distinct programs inside the operating
system)
• Concurrencybetweenthreads(“lightweightprocesses”insidethesame
program)
Definition
Athread is a single sequential flow of control within a program.
• Until this point, we have acted as if there existed a single flow of execution
within a program.
• This is rarely true! Modern programs often have many threads.
• Particularly the case for OO (Java, .NET, Smalltalk) and GUI systems
(as we’ll find out)
Threads
• Left: A single thread running within a program.
• Right: Two threads running within a program.
2
Multiple threads within the same program
How do multiple threads physically run in the same program/process?
• On computers with several CPUs/cores (i.e. almost every computer that
you’didentifyasa“computer”),eachthreadcanrunonitsownCPU/core.
Core 1 Thread 1
Core 2 Thread 2
• Threads can also run interleaved, even on a single CPU/core, where each
thread only has control periodically for a succession of time slices.
Th1 Th2 Th2 Th1 Th2 Th1
Multiple threads within the same program
• In general, the order and the size of the time slices is not predictable:
it is a dangerous mistake to assume anything less than total chaos!
• The design and coding of multi-threaded programs must involve extra
steps to ensure proper coordination, if threads are not independent.
Multiple threads within the same program
Athread can temporarily cease its execution:
Voluntarily : the thread itself issues sleep or yield commands in a fashion
known as cooperative scheduling. (Imagine cars giving way to one
another during highway merging.)
• “Selfish” threads are bad both for the system and, in the end, for
themselves.
As enforced by the OS scheduler — on systems that support so-called pre-
emptive time slicing. (Imagine lane merging with traffic lights.)
Using Threads in Java
• Implement your own threads using the system Thread or Runnable
types.
– This is the most flexible option, but it is costly. Thread creation is a
costly operation, and even “waiting” threads take up some resources
despite doing nothing.
• Use “thread pools” such as those provided by Executor or the Swing
GUI framework.
• Implicitly, using specific APIs such as Timer.
3
Java Threads and OS-supported Threads
Warning
The word “thread” is ambiguous!
• It may mean an object of type Thread: this is a Java data structure.
– One field in this structure is its run() method.
• It may also mean a locus of control in a computer system.
– Brian Goetz (Java Concurrency in Practice) calls this the “actual
thread”.
– Theoperatingsystemmayprovidedirectsupportformultiplethreads
per process.
– The JVM time-shares its OS-provided threads among its Thread ob-
jects. These objects can “come to life” only when they are paired up
with an OS-supported thread.
Java Threads and OS-supported Threads
By analogy:
• a Java thread object is like a boat captain,
• an OS-supported thread object is like a boat,
• in an OS-defined universe where,
– Captains are repeatedly shifted between boats,
– Captains pilot at most one boat at any given time, and
– Boats persist much longer than Captains.
Custom Thread class
public class SimpleThread extends Thread {
public SimpleThread(String name) {
super(name);
}
@Override
public void run() {
// ...
}
}
• Subclass Thread and override its run method, so that it does something.
• The constructor sets the Thread’s name, which could be used later, e.g.
via getName().
• The overridden run method is the heart of any Thread—also known as
its Task.
4
no reviews yet
Please Login to review.