Download Applets

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Cracking of wireless networks wikipedia , lookup

Zero-configuration networking wikipedia , lookup

Transcript
Object Serialization Explanation +
Example of file + network
Lec 22
30-Apr-17
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
2
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
3
Animation


Object diagram with stack & heap
After serialized, passed through a stream (pipe) and
reconstructed on the other side
4
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
5
How it works?

To write out an object of PersonInfo




To read that object back in



PersonInfo p = new PersonInfo();
ObjectOutputStream out;
out.writeObject(p)
ObjectInputStream in;
PersonInfo obj = (PersonInfo) in.readObject();
Must be of the same type

class and version issue
6
Example Code: Serialization
Reading/Writing PersonInfo objects
30-Apr-17
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);
}
}
8
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)
}
}
9
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)
}
}
10
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
11
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);
…………
12
Reading Objects from Network
…………..




Socket s = ss.accept();
InputStream in = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
PersonInfo p = (PersonInfo) ois.read( );
…………
13
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
14
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);
}
}
15
Circularity: not an issue

Serialization machinery will take circular references
into account and do the right thing!
Animation
16
Multithreading
30-Apr-17
Concurrency Hardware VS Software


Hardware Concurrency Headings
Software Concurrency


Processes VS Threads
Three loop code in a single class
18
Hardware Concurrency trends

Multiple CPU’s

Multiple Cores on a Single Chip

Simultaneous Multi-Threading (SMT)
19
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
20
Threads

The ability to do multiple things at once within the same
application


Lightweight


Finer granularity of concurrency
Easy to create and destroy
Shared address-space


Can share memory variables directly
May require more complex synchronization logic because of
shared address space
21
Advantages of threads…

Use multiple processors

Code is partitioned in order to be able to use n processors at once


Hide network/disk latency


While one thread is waiting for something, run the others
Dramatic improvements even with a single CPU



This is not easy to do! But Moore’s Law may force us in this direction
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
22
Why Concurrency is a Hard Problem

No language construct to alleviate the problem


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


Memory management can be solved by a garbage collector, no analog for
concurrency
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
23