Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurring Concurrently

Similar presentations


Presentation on theme: "Concurring Concurrently"— Presentation transcript:

1 Concurring Concurrently
cs205: engineering software university of virginia fall 2006 Concurring Concurrently David Evans

2 PS5 & Project PS5: Out Today Project (after PS5)
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

3 Buzzword Description [Sun95] from Class 2...
“A simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, and dynamic language.” [Sun95]

4 Multiple Threads Store Shared Data Instruction Streams

5 Our computer can only do one instruction at a time, why would we want to program pretending it can do many things at once?

6 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

7 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 }

8 Making a Thread // from PS5 SimObject class:
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?

9 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.

10 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)); }

11 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?

12 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!


Download ppt "Concurring Concurrently"

Similar presentations


Ads by Google