Download presentation
Presentation is loading. Please wait.
Published byLeony Darmadi Modified over 6 years ago
1
Object Serialization Explanation + Example of file + network
Lec 22 3-Dec-18
2
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, usman, gulberg, address.txt
3
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
4
Animation Object diagram with stack & heap
After serialized, passed through a stream (pipe) and reconstructed on the other side
5
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
6
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
7
Example Code: Serialization
Reading/Writing PersonInfo objects 3-Dec-18
8
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); }
9
Example Code: Serialization (cont.)
import java.io*; public class WriteEx{ public static void main(String args[ ]){ PersonInfo pWrite = new PersonInfo("ali", "defence", " "); 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) }
10
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) }
11
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
12
Sending Objects over Network
………….. PersonInfo p = new PersonInfo (“ali”, “defence”, “ ”); Socket s = new Socket(“localhost”, 4444); OutputStream os = s.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); oos.write(p); …………
13
Reading Objects from Network
………….. Socket s = ss.accept(); InputStream in = s.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); PersonInfo p = (PersonInfo) ois.read( ); …………
14
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
15
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); }
16
Circularity: not an issue
Serialization machinery will take circular references into account and do the right thing! Animation
17
Multithreading 3-Dec-18
18
Concurrency Hardware VS Software
Hardware Concurrency Headings Software Concurrency Processes VS Threads Three loop code in a single class
19
Hardware Concurrency trends
Multiple CPU’s Multiple Cores on a Single Chip Simultaneous Multi-Threading (SMT)
20
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
21
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
22
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
23
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.