Object Serialization Explanation + Example of file + network

Slides:



Advertisements
Similar presentations
1 Multithreaded Programming in Java. 2 Agenda Introduction Thread Applications Defining Threads Java Threads and States Examples.
Advertisements

Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
Distributed systems Programming with threads. Reviews on OS concepts Each process occupies a single address space.
File-based Persistence: Serializability COMP53 Dec 7, 2007.
CS490T Advanced Tablet Platform Applications Network Programming Evolution.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Threads CS240 Programming in C. Introduction to Threads A thread is a path execution By default, a C/C++ program has one thread called.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.
CS 390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Distributed Computing Fundamentals.
1 TCSS 360, Spring 2005 Lecture Notes Design Patterns: Singleton, Memento, Flyweight.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java I/O Writing and Reading Objects to File Serialization.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Object Persistence and Object serialization CSNB534 Asma Shakil.
1 Interfacing & embedding. 2 References  Full tutorial and references Embedding and Interfacing Manual Windows - select “Documentation” from the ECLiPSe.
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
1 Copyright © 2011 Tata Consultancy Services Limited TCS Internal.
Files and Serialization. Files Used to transfer data to and from secondary storage.
CSC 480 Software Engineering Lab 2 – Multi-Threading Oct 18, 2002.
Channels. Models for Communications Synchronous communications – E.g. Telephone call Asynchronous communications – E.g. .
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Streams. RHS – SWC 2 Binary files A binary file contains ”raw” bytes that can be interpreted as almost anything Can have any extension Can be created.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Introduction to threads
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Multithreading Lec 23.
Networking Serialization
Multi Threading.
CS 6560: Operating Systems Design
Concurrency, Processes and Threads
Operating System (013022) Dr. H. Iwidat
Chapter 4 Threads.
Client-server Programming
Memento Design Pattern
Multi-Processing in High Performance Computer Architecture:
Working with files.
Chapter 4: Threads.
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
Processes and Threads.
Units with – James tedder
Units with – James tedder
Threads and Concurrency
Channels.
Multithreaded Programming
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Chapter 4: Threads & Concurrency
Channels.
NETWORK PROGRAMMING CNET 441
Channels.
January 15, 2004 Adrienne Noble
Tutorial MutliThreading.
Concurrency, Processes and Threads
The command invocation protocol
CMSC 202 Threads.
CS18000: Problem Solving and Object-Oriented Programming
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Object Serialization Explanation + Example of file + network Lec 22 3-Dec-18

Serialization You want to send an object to a stream Motivation A lot of code involves boring conversion from a file to memory AddressBook program reads data from file and then parses it This is a common problem! ali, defence, 9201342 usman, gulberg, 5162346 address.txt

Serialization Java’s answer: Serialization Object know how to read/write themselves to streams Problem - Objects have state in memory Serialization is also called Flattening, Streaming, Dehydrate (rehydrate = read), Archiving

Animation Object diagram with stack & heap After serialized, passed through a stream (pipe) and reconstructed on the other side

Java: Automatic Serialization Serializable Interface By implementing this interface a class declares that it is willing to be read/written by automatic serialization machinery Found in java.io package Tagging interface – has no methods and serves only to identify the semantics of being serializable Automatic Writing System knows how to recursively write out the state of an object to stream Recursively follows references and writes out those objects too! Automatic Reading s System knows how to read the data from Stream and re-create object in memory Downcasting is required

How it works? To write out an object of PersonInfo PersonInfo p = new PersonInfo(); ObjectOutputStream out; out.writeObject(p) To read that object back in ObjectInputStream in; PersonInfo obj = (PersonInfo) in.readObject(); Must be of the same type class and version issue

Example Code: Serialization Reading/Writing PersonInfo objects 3-Dec-18

Example Code: Serialization import javax.swing.*; import java.io.*; public class PersonInfo implements Serializable{ String name; String address; String phoneNum; public void printPersonInfo( ){ JOptionPane.showMessageDialog( “name: ” + name + “address:” + address + “phoneNum: ” + phoneNum); }

Example Code: Serialization (cont.) import java.io*; public class WriteEx{ public static void main(String args[ ]){ PersonInfo pWrite = new PersonInfo("ali", "defence", "9201211"); try { FileOutputStream fos = new FileOutputStream("ali.dat"); ObjectOutputStream out = new ObjectOutputStream(fos); //serialization out.writeObject(pWrite); out.close(); fos.close(); } catch (Exception ex){ System.out.println(ex) }

Example Code: Serialization (cont.) import java.io*; public class ReadEx{ public static void main(String args[ ]){ try { FileInputStream fis = new FileInputStream("ali.dat"); ObjectInputStream in = new ObjectInputStream(fis); //de - serialization PersonInfo pRead = (PersonInfo) in.readObject( ); pRead.printPersonInfo(); in.close(); fis.close(); } catch (Exception ex){ System.out.println(ex) }

Object Serialization and Network You can read/write objects to network using sockets The class version should be same on both sides (client & Server) of the network

Sending Objects over Network ………….. PersonInfo p = new PersonInfo (“ali”, “defence”, “9201211”); Socket s = new Socket(“localhost”, 4444); OutputStream os = s.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); oos.write(p); …………

Reading Objects from Network ………….. Socket s = ss.accept(); InputStream in = s.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); PersonInfo p = (PersonInfo) ois.read( ); …………

Preventing Serailization transient keyword is used to mark a field that should not be serialized Often there is no need to serialize sockets, streams & DB connections etc (they do not represent the state of object, rather connections to external resources) So, we can mark them as public transient Socket s; public transient OutputStream os; public transient Connection con; Transient fields are returned as null on reading

Example Code: transient import javax.swing.*; import java.io.*; public class PersonInfo implements Serializable{ String name; String address; transient String phoneNum; public void printPersonInfo( ){ JOptionPane.showMessageDialog( “name: ” + name + “address:” + address + “phoneNum: ” + phoneNum); }

Circularity: not an issue Serialization machinery will take circular references into account and do the right thing! Animation

Multithreading 3-Dec-18

Concurrency Hardware VS Software Hardware Concurrency Headings Software Concurrency Processes VS Threads Three loop code in a single class

Hardware Concurrency trends Multiple CPU’s Multiple Cores on a Single Chip Simultaneous Multi-Threading (SMT)

Software concurrency Processes Unix-style concurrency The ability to run multiple applications at once Example: word, powerpoint , game Separate address space Cooperate using read/write streams (pipes) Synchronization is easy Since there is no shared address space

Threads The ability to do multiple things at once within the same application Finer granularity of concurrency Lightweight Easy to create and destroy Shared address-space Can share memory variables directly May require more complex synchronization logic because of shared address space

Advantages of threads… Use multiple processors Code is partitioned in order to be able to use n processors at once This is not easy to do! But Moore’s Law may force us in this direction Hide network/disk latency While one thread is waiting for something, run the others Dramatic improvements even with a single CPU Need to efficiently block the connections that are waiting, while doing useful work with the data that has arrived Writing good network codes relies on concurrency! Keeping the GUI responsive Separate worker threads from GUI thread

Why Concurrency is a Hard Problem No language construct to alleviate the problem Memory management can be solved by a garbage collector, no analog for concurrency Counter-intuitive Concurrency bugs are hard to spot in the code Difficult to get into the concurrency mindset No fixed programmer recipe either Client may need to know the internal model to use it correctly Hard to pass the Clueless-Client test Concurrency bugs are random Show up rarely, often not deterministic/reproducible easily Rule of thumb: if something bizarre happens try and note the current state as well as possible