Transcript slides

cs205: engineering software
university of virginia
fall 2006
Concurring
Concurrently
David Evans
www.cs.virginia.edu/cs205
PS5 & Project
• PS5: Out Today
– Last part of it will be done in teams of 3
– Team requests due by midnight
tomorrow (or you will be assigned
arbitrarily to a team)
– Build a distributed simulation
• Project (after PS5)
– Teams of 1-9
– Build some useful software using
principles and methods from cs205
cs205: engineering software
2
from Class 2...
Buzzword Description
“A simple, object-oriented,
distributed, interpreted,
robust, secure, architecture
neutral, portable, highperformance, multithreaded,
and dynamic language.”
[Sun95]
cs205: engineering software
3
Multiple Threads
Store
Shared Data
Instruction Streams
cs205: engineering software
4
Our computer can only do
one instruction at a time,
why would we want to
program pretending it can
do many things at once?
cs205: engineering software
5
Concurrent Programming
• Some problems are clearer to
program concurrently:
– Modularity
• Don’t have to explicitly interleave code for
different abstractions (especially: user
interfaces)
• High-level interactions – synchronization,
communication
– Modeling
• Closer map to real world problems: things in
the real world aren’t sequential
cs205: engineering software
6
Concurrency in Java
public class Thread implements Runnable {
// OVERVIEW: A thread is a thread of execution in a program.
//
The Java Virtual Machine allows an application to have
//
multiple threads of execution running concurrently.
public Thread (Runnable target)
// Creates a new Thread object that will run the target.
public void start ()
// Starts a new thread of execution.
}
… many other methods
cs205: engineering software
7
Making a
Thread
public class Thread implements Runnable {
public Thread (Runnable target)
public void start ()
… many other methods
}
// from PS5 SimObject class:
final public void init(int x, int y, Grid grid) throws BadLocationException
// REQUIRES: this is uninitialized
// MODIFIES: this
// EFFECTS: If x, y is not a valid location on grid, throws BadLocationException
//
Otherwise, initializes the cell at row, col on grid with isPaused = true.
{
this.mx = x; this.my = y; this.grid = grid; this.isPaused = true;
Thread thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
What do you know about SimObject type?
cs205: engineering software
8
Runnable
public interface Runnable {
public void run()
When an object implementing interface Runnable is
used to create a thread, starting the thread causes the
object's run method to be called in that separately
executing thread. The general contract of the
method run is that it may take any action
whatsoever.
}
So, to be a subtype of Runnable, SimObject must have a method
void run () with any postconditions it wants.
cs205: engineering software
9
Making a Runnable
abstract public class SimObject implements Runnable {
…
public void run ()
// EFFECTS: Executes one turn by calling the
// executeTurn method, and sleeps for a time
// and repeats.
{
while (true) {
executeTurn ();
delay (TURN_DELAY + random.nextInt(TURN_RANDOM));
}
}
cs205: engineering software
10
Actually…
abstract public class SimObject implements Runnable {
…
public void run ()
// REQUIRES: this has been initialized
// EFFECTS: Executes one turn by calling the
// executeTurn method, and sleeps for a time
// and repeats.
{
…
}
Hint: PS5 question 1 – is it okay to add
this requires clause?
cs205: engineering software
11
Concurrency
• Making a concurrent Java program is
easy:
Make a subtype R of Runnable
new Thread (new R ()).start ()
• Making a concurrent Java program
that behaves correctly is really, really
hard!
cs205: engineering software
12