Dining philosophers (cont)

Similar documents
Synchronized Methods of Old Versions of Java

Example: Copying the contents of a file

pre-emptive non pre-emptive

Object Oriented Programming. Week 10 Part 1 Threads

PIC 20A Streams and I/O

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

IT101. File Input and Output

CS 251 Intermediate Programming Java I/O Streams

Simple Java Input/Output

1.00 Lecture 30. Sending information to a Java program

Object-Oriented Programming Design. Topic : Streams and Files

Programming Languages and Techniques (CIS120)

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Chapter 10. IO Streams

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Software 1. Java I/O

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

Darshan Institute of Engineering & Technology for Diploma Studies

Administrivia. Events this week Drop-In Resume and Cover Letter Editing Date: Tues., Mar 23 Time: 12:30 2:30 pm Location: Rm 255, ICICS/CS

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

Programming Languages and Techniques (CIS120)

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

Software 1 with Java. The java.io package. Streams. Streams. Streams. InputStreams

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

FILE I/O. CS302 Introduction to Programming University of Wisconsin Madison Lecture 27. By Matthew Bernstein

Data Structures and Algorithms

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

I/O Streams. Object-oriented programming

CS Programming I: File Input / Output

CS 200 File Input and Output Jim Williams, PhD


Java Input/Output. 11 April 2013 OSU CSE 1

Unit 10: exception handling and file I/O

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

CS Programming I: File Input / Output

CN208 Introduction to Computer Programming

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Programming Languages and Techniques (CIS120)

Objec&ves. Review. Standard Error Streams

CS193j, Stanford Handout #26. Files and Streams

CSC 1214: Object-Oriented Programming

HST 952. Computing for Biomedical Scientists Lecture 8

Input-Output and Exception Handling

Experiment No: Group B_4

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

When we reach the line "z = x / y" the program crashes with the message:

Programming Languages and Techniques (CIS120)

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

CS Week 11. Jim Williams, PhD

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

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

File I/O Array Basics For-each loop

Text User Interfaces. Keyboard IO plus

I/O Streams. COMP 202 File Access. Standard I/O. I/O Stream Categories

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP File Access 1

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012

Unit - IV Multi-Threading

Lecture 11.1 I/O Streams

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

Software 1. תרגול 9 Java I/O

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

Sri Vidya College of Engineering & Technology Question Bank

Concurrent Programming using Threads

Files and IO, Streams. JAVA Standard Edition

Software 1. The java.io package. Streams. Streams. Streams. InputStreams

Exceptions and Working with Files

Computational Expression

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Object Oriented Software Design

Software Practice 1 - Multithreading

Object Oriented Software Design

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

File Operations in Java. File handling in java enables to read data from and write data to files

תוכנה 1 4 תרגול מס' שימוש במחלקות קיימות: קלט/פלט )IO(

Byte and Character Streams. Reading and Writing Console input and output

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Introduction Unit 4: Input, output and exceptions

Data Structures. 03 Streams & File I/O

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Administrivia. Events this week Drop-In Resume and Cover Letter Editing Date: Tues., Mar 23 Time: 12:30 2:30 pm Location: Rm 255, ICICS/CS

Java Input / Output. CSE 413, Autumn 2002 Programming Languages.

CSE 143 Lecture 25. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides adapted from Marty Stepp

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Transcription:

Administrivia Assignment #4 is out Due Thursday April 8, 10:00pm no late assignments will be accepted Sign up in labs this week for a demo time Office hour today will be cut short (11:30) Another faculty candidate interview In case you hadn t noticed Classes end Thursday April 15 th Final exam Friday Apr 23, 7:00pm BCS section: DMP 101 1 2

Dining philosophers (cont) Plan A: Pick up left fork Pick up right fork Plan B: Pick up left fork Pick up right if free Otherwise put down left and start over Plan C: P0: Pick up right fork Pick up left fork Else: Pick up left fork Pick up right fork 3 Conclusion We ve discussed how to build programs with multiple threads. To synchronize threads we can use Java s primitives: lock objects condition objects In CPSC 213 you will learn how to create independent processes on different processors and make them communicate/synchronize over a network 4

Exercises 2 nd Edition: Chapter 23, page 901 Exercises P23.1, P23.2, P23.7 3 rd Edition: Chapter 20, page 883 Exercises P20.1, P20.2, P20.5 4 th Chapter 20, page 836 Exercises P20.1, P20.2, P20.5 5 Appendix: Main Methods of class Thread public Thread() --- Allocates a new Thread object public Thread(Runnable target) public final boolean isalive() - Tests if this thread is alive public static Thread currentthread() - Get reference to currently executing thread public final String getname() public final void setname(string name) public final int getpriority() public final void setpriority(int newpriority) public void start() --- Causes thread to be scheduled; JVM calls its run() method public void run() --- If thread was constructed using a separate Runnable object, then that Runnable object's run method is called; otherwise, this method does nothing. public void interrupt() --- Interrupts this thread. public final void join() --- waits until the thread to which it is applied has died public static void sleep(long millis) --- puts currently executing thread to sleep 6 public static void yield() --- currently executing thread is temporarily paused and allow other threads to execute

Streams and Persistent Objects Reading: 2 nd Ed: 16.1-16.3, 16.5 3 rd Ed: 11.1, 19.1, 19.4 4 th Ed: 11.1, 19.1, 19.2, 19.4 http://java.sun.com/docs/books/tutorial/essential/io/streams.html 7 Learning Objectives describe the stream abstraction used in Java for byte and character input/output write programs that use streams to read and write data incorporate data persistence in a program using Java's serialization mechanism 8

Input and Output We know how to read input from the "standard input stream", and to write output to the "standard output stream": How do we read text that is input by the user? Scanner in = new Scanner(System.in); String line = in.nextline(); How do we output text for the user? System.out.println(line); What if we want to read text from a file, and write text to a 9 file? Reading Text Files The easiest way to read text from a file is to use the Scanner class. This allows us to read any of the primitive types (i.e., int, double, string etc. ) from a text file. Scanner is defined in java.util and has the following behaviour: has a constructor that creates a scanner from a specified stream has methods like next, nextline, nextint, NextDouble, etc. to read data for java s built-in types Example: Scanner in = new Scanner( new FileReader( input.txt ) ); int x = in.nextint(); String s = in.next(); 10

Writing Text Files The easy way to write values of primitive types to a file is to use a PrintWriter A PrintWriter can be directly associated with a file or with an OutputStream has a constructor with the file name as parameter has methods print and println which accept any of the primitive types and convert their values to strings print and println convert general objects to strings using tostring() Example: PrintWriter out = new PrintWriter( output.txt ); out.println(123.05); out.println( Hello World ); out.println( new Account( john, 100) ); 11 What Are These Streams? What exactly are "FileReader" and "PrintWriter", "Reader", "OutputStream", and for that matter, "System.in", and "System.out"? FileReader and PrintWriter are examples of streams. System.in is also a stream, it's an instance of InputStream System.out is also a stream, it's an instance of PrintStream 12

What are streams? Most programs exchange data either with other programs, or with devices, or both e.g., the myubc portal must load information about who you are when you login (likely from a database) e.g., your web browser remembers recently visited sites, cookies, etc. on the local file system e.g., if you Google a term, you are sending data to a web server somewhere in the world and it is returning data to your web browser We use streams to abstract the concept of data flowing from one program/device to another Each stream has a source (from which data flows) and a sink (into which data flows) source Data flows in FIFO order sink 13 Using a Stream To communicate with another program/device/file, a program creates and opens a stream to (or from) the other program/device/file transfers data through the stream (usually, one piece at a time) closes the stream 14

Java Stream Types Java provides many stream classes (in the java.io package) to support I/O from devices with different characteristics These stream classes are all used in basically the same way (it does not matter if the stream goes to a file, another computer, or somewhere else). Two basic categories: Character streams, which are used to communicate (16-bit) characters in a platform independent way Byte streams, which are a sequence of (8-bit) bytes used to read/write binary data (images, sound), manipulate raw files, and for object serialization 15 Stream Classes Character Input Streams: FileReader: input comes from a file. Example: Reader r = new FileReader("filename.txt"); StringReader: input comes from a string. Example: Reader r = new StringReader("this is input"); CharArrayReader: input comes from an array of char. Example: char[] myarray = { i, n, p, u, t }; Reader r = new CharArrayReader(myArray); 16

Stream Classes Character Output Streams: same names, replacing Reader by Writer. There is one additional useful class: PrintWriter: has print() and println() methods Byte Input and Output Streams: same names, using InputStream and OutputStream instead of Reader and Writer For more detail, see appendix at the end of these lecture notes 17 Example: Copying the contents of a file Copy the contents of the file recentphoto.jpg to a new file called mydog.jpg: int data; try { FileInputStream in = new FileInputStream("recentPhoto.jpg"); FileOutputStream out = new FileOutputStream("myDog.jpg"); while ((data = in.read())!= -1) out.write(data); returns -1 in.close(); when end of out.close(); file is reached } catch (IOException e) { System.err.println("File error: " + e.getmessage()); } 18