Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 SJSU Dept. of Computer Science Spring 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.

3 SJSU Dept. of Computer Science Spring 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! _

4 SJSU Dept. of Computer Science Spring 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. _

5 SJSU Dept. of Computer Science Spring 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.

6 SJSU Dept. of Computer Science Spring 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. _

7 SJSU Dept. of Computer Science Spring 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. _

8 SJSU Dept. of Computer Science Spring 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. _

9 SJSU Dept. of Computer Science Spring 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.

10 SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 10 Multithreaded Café: Design Questions  How to represent the cafe?  How to represent customers?

11 SJSU Dept. of Computer Science Spring 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?

12 SJSU Dept. of Computer Science Spring 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?

13 SJSU Dept. of Computer Science Spring 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. _

14 SJSU Dept. of Computer Science Spring 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);

15 SJSU Dept. of Computer Science Spring 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()

16 SJSU Dept. of Computer Science Spring 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

17 SJSU Dept. of Computer Science Spring 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. _


Download ppt "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."

Similar presentations


Ads by Google