CS180 Review Recitation Week 15
Announcement Final exam will be held on Thursday(12/17) 8:00~10:00 AM The coverage is comprehensive Project 5 is graded. Check your score in Blackboard.
Classes and Methods Visibility modifier Scope: public > protected > none > private If there s no modifier, it s package-private. Visible to any class in the same package. Static method Static method can access only the class variables and the class constants. It can be called by using dot notation with the class name.
Inheritance and Polymorphism Polymorphism versus overloading The binding of a polymorphic method is decided at runtime. The binding of an overloading method is decided at compile time
Exception Checked exceptions must be handled explicitly. These exceptions can be analyzed and discovered during compile time. Will not compile if not handled. Unchecked exceptions NullPointerException, ArithmeticException, IndexOutOfBoundsException finally(){.. is always executed. Even if there is a return from try block or catch block, the finally block is always executed.
Concurrency - Thread States new thread start() Runnable notify() wake up Not Runnable schedule yield() (un)schedule Running sleep() wait() run() terminates uncaught exception terminated
Concurrency Thread Example public class DisplayNames{ public static void main( String[] args ){ import java.util.random; DisplayMe task1 = new DisplayMe( "thread1" ); class DisplayMe extends Thread { DisplayMe task2 = new DisplayMe( "thread2" ); System.out.println( "Starting threads" ); task1.start() task2.start(); try{ task1.join(); task2.join(); catch(interruptedexception e){ start() enables the thread System.out.println( main() is done!\n" ); Join() blocks until the thread completes. private int sleeptime; private String name; private static Random generator = new Random(); public DisplayMe( String name ) { this.name = name; sleeptime = generator.nextint( 5000 ); public void run() { try { Thread.sleep( sleeptime ); run() is where the thread actually executes catch ( InterruptedException exception ){ exception.printstacktrace(); System.out.printf( "%s done sleeping\n", name ); sleep() puts the thread into Not Runnable state
Synchronization Race condition Situation where the execution order of threads affects the result. Critical section Use synchronized block when accessing shared data Deadlock Multiple threads wait forever for others to complete. Solution: lock things in the same order
Synchronization wait() give up the lock Notify wake up waiting threads notifyall() wake up all waiting threads Surround these three calls with synchronized blocks Otherwise, will get IllegalMonitorStateException
Synchronization Examples class Box { private final Object lock = new Object(); private Object item = null; public Object get() throws InterruptedException { synchronized (lock) { while (item == null) { lock.wait(); Object r = item; item = null; lock.notifyall(); return r; Need to synchronize on the same object for which you call wait() or notify() public void put(object o) throws InterruptedException { synchronized (lock) { while (item!= null) { lock.wait(); item = o; lock.notifyall();
GUI Layout Layout manager handles the window layout automatically. FlowLayout, GridLayout, BorderLayout etc Event handling Keyboard event, mouse event, action event ActionListener interface includes a method actionperformed() which captures the event.
Abstract Data Types ADT defines the behavior of the data structure. Linked list A chain of nodes. Used to implement ADTs. Stack First-In Last-Out Queue First-In First-Out
File File I/O is similar to networks in Java Can write/read Serializable objects by ObjectInputStream and ObjctOutputStream
Networking If the client outputs with an ObjectOutputStream, server needs to receive it with an ObjectInputStream. Vice versa. The output stream does not send data immediately, until a flsh() or close() is called.
Recursion A function containing a call to itself Solve problems by breaking the problem into smaller, more easily solve subproblems. Merge sort, factorial, Tower of Hanoi, etc Infinite-Recursion Wrong base case Wrong recursion input size(e.g., the problem input is not getting smaller)
Recursion Merge Sort If the size of the list to be sorted is one or empty, it is already sorted. Otherwise, split the list into two half. Sort the two half separately. Merge the two sublists into one list.
Generics Prevent code duplication Preserve type safety. Type parameter (e.g. ArrayList<String> strlist;) Bounded type parameter Constrain the object type of the parameter.
Generics - Examples class Box<E extends Boxable> { private E item = null; public E get() {... item.unbox();... public void put(e o) {... item.box();... Only the classes that extend from Boxable are allowed
Quiz class MyThread{ Object lock; synchronized(this){ lock.wait(); What is wrong with it?