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