Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Similar presentations


Presentation on theme: "CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

1 CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 2 CS Department Colloquium  Dr. Richard Stallman President, Free Software Foundation Developer of the GNU operating system MacArthur Foundation fellowship  Talk: “For a Free Digital Society” About the many threats to freedom in the digital society.  Thursday, November 21, 4:30 PM  Washington Square Hall Room 109

3 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 3 Type Bounds  Tell the compiler that MyType is bound (constrained) to be a subclass of class SimpleShape. private static MyType max(MyType elements[]) { int maxIndex = 0; for (int i = 1; i < elements.length; i++) { if (elements[i].compareTo(elements[maxIndex]) > 0) { maxIndex = i; } return elements[maxIndex]; }

4 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 4 Type Bounds and Wildcards  ? super MyType says that whatever type you pass to max(), that type must implement Comparable where T is the superclass of that type. Example: You pass a Square array. Then Square must implement Comparable  SimpleShape is the superclass of Square. _ private static > MyType max(MyType elements[]) {... }

5 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 5 Generic Methods  A generic method has one or more type parameters.  Then the method can use a type parameter: As the return type. As the type of parameters. As the type of local variables.  Example: Method first() takes an array of elements of some type and returns the first array element. Use type parameter : public MyType first(MyType elements[]) { MyType f = elements[0]; return f; }

6 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 6 Type Erasure  Generic types were tacked onto Java 1.5.  Generics is strictly a compile time process. After type checking, the generic types are “erased”. At run time, the Java virtual machine knows nothing about generic classes or methods.  After type erasure, the Java compiler uses raw types. It replaces type variables with ordinary Java types. It obtains the raw type of a generic type by erasing the type variables. Each type variable is replaced by its bound, or with Object if it is not bounded. _

7 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 7 Type Erasure  Example: The generic class ArrayList becomes  Example: The generic method becomes The type bound > is erased to just Comparable. public class ArrayList { private Object[] elementData; public Object get(int i) {... } public Object set(int i, Object newValue) {... }... } public static > E getMax(ArrayList a) public static Comparable getMax(ArrayList a)

8 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 8 JUnit Magic  Recall that to use JUnit to do unit testing, you Create a test class that contains test methods. Each test method has a name that starts with test.  Example: Test class for class Day : import junit.framework.*; public class DayTest extends TestCase { public void testAdd() {... } public void testAddNegative() {... } public void testDaysFrom() {... } public void testGregorianBoundary () {... }

9 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 9 JUnit Magic, cont’d  How did JUnit find at run time all the methods whose names started with test ?  How did JUnit invoke those methods? The JUnit code could not have known ahead of time the names of the test methods (other than they start with test ). _

10 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 10 Reflection  JUnit uses a feature of Java called reflection.  Reflection is a mechanism that allows a program (such as a JUnit test) to Discover information about itself at runtime, such as the names of all the methods in the test class. Use that information, such as calling the methods whose names start with test.  Reflection allows a program to analyze its objects and their capabilities at run time. This “self examination” is a powerful feature! _

11 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 11 Reflection Metadata Classes  Reflection uses several metadata descriptor classes. metadata = data about data Class Package Field Method Constructor Array

12 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 12 Inspect an Array  Use the static methods of the Array class.  Suppose arr refers to an array. Read its i th value:  Set its i th value:  Get its length:  Construct a new array: Object value = Array.get(arr, i); Array.set(arr, i, value); int n = Array.getLength(arr); Object arr = Array.newInstance(type, length);

13 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 13 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.  What’s the difference between a thread and a 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. Threads run within a single process.  Process resources such as memory are shared.  Thread switching is very fast.

14 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 14 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 processes each event as it arrives. One long event process prevents other events from being processed. Solution: The event listener passes event processing to a “worker thread” and resumes polling for events.  Potentially simpler program design. _

15 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 15 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?  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. _

16 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 16 Multicores and Multithreaded Programming  Multicore processors have become 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. 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 an advanced topic.  CS 159 Introduction to Parallel Processing Soon, we will teach multithreaded programming starting in the beginning classes.  Better language support is needed, though.

17 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 17 Multithreaded GUI Programming  If you have done GUI programming such as with Swing, you have already done multithreaded programming! Main thread: The main method and its call chain. Event queue thread: All the event handlers, such as button clicks, menu selections, etc.  Luckily, your main thread and the event queue thread probably didn’t need to interact with each other. _ Demo

18 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 18 How to Create a Thread  Define a class that implements the Runnable interface:  Put the code that you want to run in parallel with other code in the run() method. public interface Runnable { void run(); } public class GreetingProducer implements Runnable {... public void run() { // Code to run in parallel here. }

19 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 19 Runnable Example  Repeatedly print a greeting.  Thread.sleep() yields control to another thread. Can throw the InterruptedException exception. public void run() { try { for (int i = 1; i <= REPETITIONS; i++) { System.out.println(i + ": " + greeting); Thread.sleep(DELAY); } catch (InterruptedException exception) { // do nothing }

20 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 20 Runnable Example, cont’d  Create a Runnable object of the class.  Construct a Thread object using the Runnable object.  Call the start() method of the Thread object.  Example: Create two threads. Each thread loops to repeatedly print a greeting. Runnable r1 = new GreetingProducer("Hello, World!"); Runnable r2 = new GreetingProducer("Goodbye, World!"); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); Demo

21 SJSU Dept. of Computer Science Fall 2013: November 19 CS 151: Object-Oriented Design © R. Mak 21 Runnable Example, cont’d  The output may not always be perfectly interleaved! The thread scheduler does not guarantee the order that the threads will execute. 1: Hello, World! 1: Goodbye, World! 2: Hello, World! 2: Goodbye, World! 3: Hello, World! 3: Goodbye, World! 4: Hello, World! 4: Goodbye, World! 5: Hello, World! 5: Goodbye, World! 6: Hello, World! 6: Goodbye, World! 7: Hello, World! 7: Goodbye, World! 8: Goodbye, World! 8: Hello, World! 9: Goodbye, World! 9: Hello, World! 10: Goodbye, World! 10: Hello, World!


Download ppt "CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google