CS180 Review. Recitation Week 15

Similar documents
Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CS 351 Design of Large Programs Threads and Concurrency

Java Threads. COMP 585 Noteset #2 1

Java Threads. Written by John Bell for CS 342, Spring 2018

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Problems with Concurrency. February 19, 2014

cs Java: lecture #6

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

Multithreading Pearson Education, Inc. All rights reserved.

Performance Throughput Utilization of system resources

CMSC 132: Object-Oriented Programming II. Threads in Java

CS 180 Final Exam Review 12/(11, 12)/08

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

Synchronization in Concurrent Programming. Amit Gupta

CS 556 Distributed Systems

CS360 Lecture 12 Multithreading

1 Constructors and Inheritance (4 minutes, 2 points)

Threads Chate Patanothai

Contents. 6-1 Copyright (c) N. Afshartous

Synchronization Lecture 23 Fall 2017

Multithreaded Programming

Animation Part 2: MoveableShape interface & Multithreading

Java Threads. Introduction to Java Threads

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Threads and Parallelism in Java

What is a thread anyway?

Concurrent Programming using Threads

7. MULTITHREDED PROGRAMMING

Multiple Inheritance. Computer object can be viewed as

CSCI 201L Final Written SOLUTION. 13% of course grade

RACE CONDITIONS AND SYNCHRONIZATION

Component-Based Software Engineering

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

Programming Language Concepts: Lecture 11

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

CSCD 330 Network Programming

Advanced Concepts of Programming

JAVA MOCK TEST JAVA MOCK TEST IV

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Concurrency in Java Prof. Stephen A. Edwards

Multithread Computing

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

User Space Multithreading. Computer Science, University of Warwick

Java s Implementation of Concurrency, and how to use it in our applications.

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CS18000: Programming I

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

JAVA MOCK TEST JAVA MOCK TEST III

Threads in Java (Deitel & Deitel)

CSCD 330 Network Programming

Java Programming. Price $ (inc GST)

What is a Thread? Why Multicore? What is a Thread? But a fast computer runs hot THREADS AND CONCURRENCY. Concurrency (aka Multitasking)

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

Introduction to Java Threads

CS2013 Course Syllabus Spring 2017 Lecture: Friday 8:00 A.M. 9:40 A.M. Lab: Friday 9:40 A.M. 12:00 Noon

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Multi-threading in Java. Jeff HUANG

VALLIAMMAI ENGINEERING COLLEGE

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

CS11 Java. Fall Lecture 7

Synchronization in Java

Multithreading. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

CS18000: Programming I Final Exam 8 May 8am-10am Lilly Functional Abstraction. Functional Specification. CS18000: Programming I 4/28/2010

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

COMP346 Winter Tutorial 4 Synchronization Semaphores

CS18000: Programming I

Threads Questions Important Questions

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Definition: A thread is a single sequential flow of control within a program.

Assigned Date: August 27, 2014 Due Date: September 7, 2015, 11:59 PM

15CS45 : OBJECT ORIENTED CONCEPTS

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Java Threads Instruct or: M Maina k Ch k Chaudh dh i ur ac.in in 1

Exercise Session Week 8

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CMSC132, Practice Questions

Core Java Syllabus DAY -1 :

Monitors; Software Transactional Memory

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Part IV Other Systems: I Java Threads

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Exercise Session Week 8

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Principles of Software Construction: Concurrency, Part 2

CS/ENGRD2110: Final Exam

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

Concurrency Quiz: Java Threads

Programming II (CS300)

Self-test Java Programming

Core Java Contents. Duration: 25 Hours (1 Month)

"Charting the Course... Java Programming Language. Course Summary

Fall Problem Set 1

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

CSC 1351: Quiz 6: Sort and Search

THREADS AND CONCURRENCY

Concurrent and Real-Time Programming in Java

Transcription:

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?