cs 152: programming language paradigms may 7 class meeting department of computer science san jose...

17
CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www.cs.sjsu.edu /~mak

Upload: grant-collins

Post on 02-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

CS 152: Programming Language Paradigms

May 7 Class Meeting

Department of Computer ScienceSan Jose State University

Spring 2014Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

2

Multithreaded Programming is Hard!

The execution order of the threads is not guaranteed.

Your program must work no matter what the order is. You must explicitly synchronize the threads. A thread must remember to signal other threads

to prevent the program from deadlocking.

It is easy to corrupt shared objects.

A thread must prevent another thread from “sneaking in” before it is finished with its operations.

You must manage critical regions.

Page 3: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

3

Multithreaded Programming is Hard! cont’d

Debugging multithreaded programs is difficult.

Different errors (or none at all) may occur on each run.

Errors may be timing related.

However, multithreaded programming is a critical job skill.

Learn multiprogramming or be obsolete!_

Page 4: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

4

Class Exercise: Multithreaded Café

A small busy café serves coffee and burgers.

At most three customers at a time can be inside the café .

Only one server is working behind the counter.

Customers are served in the order that they enter._

Page 5: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

5

Multithreaded Café, cont’d

Coffee customers enter through any one of three coffee doors.

At least one every minute. Once a server starts serving a coffee customer,

it takes up to two minutes.

Burger customers enter through the one burger door.

At least one every five minutes. Once a server starts serving a burger customer,

it takes up to five minutes.

Page 6: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

6

Multithreaded Café: The Burger Rule

Once a burger customer enters, the server needs to concentrate on making the burger.

Other customers already in the café can leave after they’ve been served.

No other customers may enter until the burger customer is served and leaves._

Page 7: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

7

Multithreaded Café: Java Program

Write a multithreaded Java program to simulate the activities of the café.

One second of simulation run time equals one minute of cafe activity._

Page 8: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

8

Multithreaded Café: Java Program, cont’d

Run the simulation until

5 coffee customers have entered through each coffee entrance and have been served.

5 burger customers have entered through the burger entrance and have been served._

Page 9: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

9

Multithreaded Café: Java Program, cont’d

Print a line for each event as it occurs. An event is a coffee or burger customer

entering or leaving.

Include in each printed line:

Timestamp of elapsed simulated minutes and seconds.

The current number of customers in the café. The event and its sequence number.

You can print a negative sequence number for a leaving event.

Page 10: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

10

Multithreaded Café: Design Questions

How to represent the cafe? How to represent customers?

Page 11: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

11

Multithreaded Café: Design Questions, cont’d

What threads? Locks? Condition objects? Shared data? Critical regions?

Page 12: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

12

Multithreaded Café: Design Questions, cont’d

How to implement the burger rule? How to start the simulation?

Page 13: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

13

Message Passing

Message passing is another mechanism for thread synchronization and communication using the distributed model of parallel processing.

Threads are synchronized by waiting for messages to arrive._

Page 14: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

14

Message Passing

In its most basic form, message passing consists of two operations, send and receive.

Example C code:

(Assumes every sender knows its receiver and vice versa.)

void send(Process to, Message msg);void receive(Process from, Message msg);

Page 15: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

15

Message Passing: Go Language

Google’s Go language has built-in support for parallel programming via message passing.

The go statement starts a goroutine (thread).

Example:

starts function func as a goroutine.

go func()

Page 16: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

16

Message Passing: Go Language

A channel type provides a synchronized channel between goroutines.

Example:

Send a message msg over channel ch:

Receive a message from channel ch:

Both block until the other goroutine is ready to communicate.

chan ch

ch <- msg

<- ch

Page 17: CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceSpring 2014: May 7

CS 152: Programming Language Paradigms© R. Mak

17

Message Passing: Go Language

The select statement waits on a set of channels for communication from any channel._