Download presentation
Presentation is loading. Please wait.
Published byMelvyn Blankenship Modified over 8 years ago
1
CS 152: Programming Language Paradigms April 28 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: April 28 CS 152: Programming Language Paradigms © R. Mak 2 Pass by Name Originally implemented by Algol 60 as a way to do inline procedures. Replace the name of a formal parameter used within a procedure by the text of the actual parameter. A lazy evaluation mechanism. The actual parameter is not evaluated until the name of the corresponding formal parameter is encountered at run time. _
3
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 3 Pass by Name, cont’d Suppose C used pass by name. What happens to array a ? int i; int a[2]; void inc(int x) { i++; x++; } int main() { i = 0; a[0] = 0; a[1] = 1; inc(a[i]); return 0; } int i; int a[2]; void inc(int x) { i++; a[i]++; } int main() { i = 0; a[0] = 0; a[1] = 1; inc(a[i]); return 0; } a[0] = 0 a[1] = 2 Pass-by-name parameters are evaluated as small functions called “thunks”. A thunk executes in the context of the caller.
4
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 4 Pass by Name, cont’d Pass by name: What value will be printed? int i; int p(int y) { int j = y; i++; return j + y; } void q() { int j = 2; i = 0; printf("%d\n", p(i + j)); } main() { q(); } int i; int p(int y) { int j = i + j; i++; return j + i + j; } void q() { int j = 2; i = 0; printf("%d\n", p(i + j)); } main() { q(); } 022 1 2 2 5
5
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 5 Pass by Name, cont’d Pass by name: Does this swap procedure work? void swap(int x, int y) { int temp = x; x = y; y = temp; } int i; int j; swap(i, j); int i; int a[10]; swap(i, a[i]); int temp = i; i = j; j = temp; int temp = i; i = a[i]; a[i] = temp; Yes! Oops!
6
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 6 Data Types Recall that a data type specifies: The collection of values that belong to the type. The operations that can be performed on those values. _
7
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 7 Data Types, cont’d In a strongly typed language, variables have types. Types are checked for assignment and comparison compatibility at compile time by the parser. Most type errors are caught at compile time, but some aren’t caught until run time. Strongly typed languages lead to “safer” programs but often place greater burdens on the programmers.
8
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 8 Logical and Physical Forms Data types have a logical and a physical form. The logical form embodies the concept of the data type. Example: The concept of an array. A data structure consisting of a collection of components all of the same type, where each component is uniquely accessed via an integral index value. _
9
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 9 Logical and Physical Forms, cont’d The physical form is the implementation of the data type within a computer program. Example: Implement an array either as: A contiguous block of memory where the index value is an offset from the starting address of the block to the desired component. A sparse matrix where an index value accesses a pointer to the desired component in memory. _
10
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 10 Abstract Data Types An abstract data type (ADT) is the realization of a data type in software. The interface of the ADT is defined in terms of a collection of values of the type and the set of operations. The implementation details are hidden from the users of the ADT through a concept known as encapsulation. _
11
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 11 Encapsulation Encapsulation allows the design and implementation details of an ADT to change without affecting its users, as long as the interface does not change. Programmers who use an ADT should only think in terms of the operations on the data type without knowledge of the implementation. _
12
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 12 Language Support for ADTs Modern languages support programmers to create ADTs. Allow programmers to define data type and its operations, with encapsulation and information hiding mechanisms to insulate users from implementation details. Build ADTs from previously-defined types. Provide language constructs that enable all the implementation details of an ADT to be defined in one place and which expose only the interfaces to users.
13
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 13 Key Design Goals of an ADT Modifiability Reusability Security _
14
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 14 Example ADT: Logical Specification of a List Maintain an ordered collection of elements. Primitive or a reference type elements. All elements will have the same type. Access the elements of a list either Sequentially, one after another starting from a given element and ending with another given element. Randomly, via each element’s index. Each list has a size, which is the number of elements that it contains. In particular, the empty list has size 0.
15
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 15 List Operations Return the size of the list. Add an element to the end of the list. Clear the list (remove all of its elements). At a given position of the list: Insert an element. Retrieve the element. Modify the element. Remove (and return) the element. _
16
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 16 List Implementation A list can be implemented as an array or as a linked list. Encapsulate and hide the implementation from list users (other programmers). _
17
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 17 Sample List Implementation in Java Implementation in package ListImplementation. Interface List provides the interface for list users. Implement as an array. _ package ListImplementation; public interface List { int size(); void add(AnyType element); void insert(int index, AnyType element); AnyType get(int index); void set(int index, AnyType element); AnyType remove(int index); void clear(); }
18
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 18 Implement as an Array package ListImplementation; public class ListAsArray implements List { private static final int INITIAL_ALLOCATION = 10; private int size; private AnyType data[] = (AnyType[]) new Object[INITIAL_ALLOCATION]; private int currentAllocation() { return data.length; } private void doubleAllocation() { AnyType newData[] = (AnyType[]) new Object[2*data.length]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; }... }
19
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 19 Implement as an Array, cont’d package ListImplementation; public class ListAsArray implements List {... @Override public int size() { return size; } @Override public void add(AnyType newElmt) { if (size >= currentAllocation()) doubleAllocation(); data[size++] = newElmt; }... }
20
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 20 Implement as an Array, cont’d package ListImplementation; public class ListAsArray implements List {... @Override public void insert(int index, AnyType newElmt) { if ((index = size)) { throw new IndexOutOfBoundsException(); } if (size >= currentAllocation()) doubleAllocation(); for (int i = size-1; i >= index; i--) { data[i+1] = data[i]; } data[index] = newElmt; size++; }... }
21
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 21 Implement as an Array, cont’d package ListImplementation; public class ListAsArray implements List {... @Override public AnyType get(int index) { if ((index = size)) { throw new IndexOutOfBoundsException(); } return data[index]; } @Override public void set(int index, AnyType newElmt) { if ((index = size)) { throw new IndexOutOfBoundsException(); } data[index] = newElmt; }... }
22
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 22 Implement as an Array, cont’d package ListImplementation; public class ListAsArray implements List {... @Override public AnyType remove(int index) { if ((index = size)) { throw new IndexOutOfBoundsException(); } AnyType removedElmt = data[index]; for (int i = index; i < size-1; i++) { data[i] = data[i+1]; } size--; return removedElmt; }... }
23
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 23 Implement as an Array, cont’d package ListImplementation; public class ListAsArray implements List {... @Override public void clear() { size = 0; } }
24
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 24 Assignment #5: A Solution
25
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 25 Building a Parse Tree Recursively private Node parseList() { Node root = new Node(); Node currentNode = null; Token token = nextToken(); TokenType tokenType = token.getType(); while (tokenType != TokenType.SS_RPAREN) { if (currentNode == null) { currentNode = root; } else { Node newNode = new Node(); currentNode.setCdr(newNode); currentNode = newNode; } if (tokenType == TokenType.SS_LPAREN) { currentNode.setCar(parseList()); } else { currentNode.setToken(token); } token = nextToken(); tokenType = token.getType(); } return root; // of the parse tree } Call parseList() to parse a list. The ( token has already been seen. Build a binary tree representing the list and return the root of the tree. Call parseList() recursively to parse a sublist whenever the token is (.
26
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 26 Building a Parse Tree Recursively private Node parseList() { Node root = new Node(); Node currentNode = null; Token token = nextToken(); TokenType tokenType = token.getType(); while (tokenType != TokenType.SS_RPAREN) { if (currentNode == null) { currentNode = root; } else { Node newNode = new Node(); currentNode.setCdr(newNode); currentNode = newNode; } if (tokenType == TokenType.SS_LPAREN) { currentNode.setCar(parseList()); } else { currentNode.setToken(token); } token = nextToken(); tokenType = token.getType(); } return root; // of the parse tree } Create the root of the parse tree. currentNode will be the node being worked on. Get the next token after the ( The first time through the loop, currentNode is the root node. Subsequently, each iteration moves currentNode down the cdr links of the parse tree. If the token is a (, recursively parse the sublist and return the subtree root node. Then currentNode adopts that root node as its car. Otherwise, if it’s just an element, currentNode sets the element in its data field. Get the next token and loop again.
27
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 27 Building a Parse Tree Recursively private Node parseList() { Node root = new Node(); Node currentNode = null; Token token = nextToken(); TokenType tokenType = token.getType(); while (tokenType != TokenType.SS_RPAREN) { if (currentNode == null) { currentNode = root; } else { Node newNode = new Node(); currentNode.setCdr(newNode); currentNode = newNode; } if (tokenType == TokenType.SS_LPAREN) { currentNode.setCar(parseList()); } else { currentNode.setToken(token); } token = nextToken(); tokenType = token.getType(); } return root; // of the parse tree } (1 2 3) root currentNode 1 2 3 (a (1 2 3) b) root currentNode a b 1 2 3
28
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 28 Multithreaded Programming in Java Multithreaded programming involves having multiple threads of execution happening concurrently within a single Java program. A thread is a program unit that executes independently of other parts of the program. _
29
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 29 Thread vs. Process A process is an executing program managed by the operating system. A process has its own resources, such as its separate runtime stack and its separate share of main memory. Switching between processes is relatively slow. A thread runs within a single process. Process resources such as memory are shared. Thread switching is very fast.
30
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 30 Advantages of Multithreaded Programming Better resource utilization. Example: When one thread is waiting for disk I/O to complete, another thread can be doing some work. More responsive programs. Example: An event listener polls multiple event sources and handles each event as it arrives. One long event handler prevents other events from being handled. Solution: The event listener passes event handling to a “worker thread” and resumes polling for events. Potentially simpler program design.
31
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 31 Challenges of Multithreaded Programming Nondeterministic order of thread execution. You must ensure that the behavior of your program is not affected by execution order. Thread synchronization. One thread may need the results computed by another thread. Access to shared objects. What if two separate threads simultaneously attempt to modify a shared object? _
32
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 32 Challenges, cont’d Deadlocks Two threads each wait for the other to complete a task and your program waits indefinitely. Greater overhead. Managing multithreading takes up extra machine cycles. Is it worth it? Debugging woes. A multithreaded program can be extremely hard to debug.
33
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 33 Multicores and Multithreaded Programming Multicore processors are now common. Multithreaded programming is necessary to take advantage of having multiple cores. A programmer who only knows how to do single-threaded programming will soon become obsolete. _
34
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 34 Multithreaded Programming, cont’d Object-oriented programming used to be an advanced topic. Today, we teach object-oriented programming starting in the beginning programming classes. Languages like Java help make that possible. Today, multithreaded programming is still an advanced topic. CS 159 Introduction to Parallel Processing _
35
SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 35 Multithreaded Programming, cont’d Soon, we will teach multithreaded programming starting in the beginning classes. Better language support is needed, though. _
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.