CS 152 Programming Language Paradigms May 7 Class
- Slides: 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
Multithreaded Programming is Hard! o The execution order of the threads is not guaranteed. n n n o Your program must work no matter what the order is. You must explicitly synchronize threads. A thread must remember to signal other threads to prevent the program from deadlocking. It is easy to corrupt shared objects. n n A thread must prevent another thread from “sneaking in” before it is finished with its operations. You must manage critical regions. SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 2
Multithreaded Programming is Hard! cont’d o Debugging multithreaded programs is difficult. n n o Different errors (or none at all) may occur on each run. Errors may be timing related. However, multithreaded programming is a critical job skill. n Learn multiprogramming or be obsolete! _ SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 3
Class Exercise: Multithreaded Café o A small busy café serves coffee and burgers. o At most three customers at a time can be inside the café. o Only one server is working behind the counter. o Customers are served in the order that they enter. _ SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 4
Multithreaded Café, cont’d o Coffee customers enter through any one of three coffee doors. n n o 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. n n At least one every five minutes. Once a server starts serving a burger customer, it takes up to five minutes. SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 5
Multithreaded Café: The Burger Rule o Once a burger customer enters, the server needs to concentrate on making the burger. o Other customers already in the café can leave after they’ve been served. o No other customers may enter until the burger customer is served and leaves. _ SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 6
Multithreaded Café: Java Program o Write a multithreaded Java program to simulate the activities of the café. o One second of simulation run time equals one minute of cafe activity. _ SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 7
Multithreaded Café: Java Program, cont’d o Run the simulation until n 5 coffee customers have entered through each coffee entrance and have been served. n 5 burger customers have entered through the burger entrance and have been served. _ SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 8
Multithreaded Café: Java Program, cont’d o Print a line for each event as it occurs. n o An event is a coffee or burger customer entering or leaving. Include in each printed line: n n n Timestamp of elapsed simulated minutes and seconds. The current number of customers in the café. The event and its sequence number. o You can print a negative sequence number for a leaving event. SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 9
Multithreaded Café: Design Questions o o How to represent the cafe? How to represent customers? SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 10
Multithreaded Café: Design Questions, cont’d o o o What threads? Locks? Condition objects? Shared data? Critical regions? SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 11
Multithreaded Café: Design Questions, cont’d o o How to implement the burger rule? How to start the simulation? SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 12
Message Passing o Message passing is another mechanism for thread synchronization and communication using the distributed model of parallel processing. o Threads are synchronized by waiting for messages to arrive. _ SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 13
Message Passing o In its most basic form, message passing consists of two operations, send and receive. n Example C code: void send(Process to, Message msg); void receive(Process from, Message msg); (Assumes every sender knows its receiver and vice versa. ) SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 14
Message Passing: Go Language o Google’s Go language has built-in support for parallel programming via message passing. o The go statement starts a goroutine (thread). n Example: go func() starts function func as a goroutine. SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 15
Message Passing: Go Language o A channel type provides a synchronized channel between goroutines. n Example: n Send a message msg over channel ch: chan ch ch <- msg n Receive a message from channel ch: <- ch n Both block until the other goroutine is ready to communicate. SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 16
Message Passing: Go Language o The select statement waits on a set of channels for communication from any channel. _ SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 17
- R programming language paradigms
- Binding in programming paradigms
- Ktu programming paradigms notes
- Hardware description language
- Unr 152
- Ntuser.dat forensics
- Przedszkole 152 łódź
- Re liveri [2006] qca 152
- Rounding hundred thousands
- Mae 152
- Mae 152
- Cs 152 stanford
- Cs 152 berkeley
- Ba 152
- Ece 152
- Ba 152
- Econ 152
- Which layer of the osi model includes vlans