Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Tests sur la serialisation
  • Loading branch information
guillaumemoreau committed Oct 16, 2012
1 parent e7ce34a commit b485909
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
38 changes: 38 additions & 0 deletions SerA.java
@@ -0,0 +1,38 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public class SerA {
protected int x;
protected int y;
}

class SerB extends SerA implements java.io.Serializable {
protected int c;

}

class SerTest {
public static void main(String[] args) throws java.io.IOException,ClassNotFoundException {
SerB s = new SerB();
s.x = 9; s.y = 12; s.c = -2;
FileOutputStream fOut = new FileOutputStream("serB.ser");
ObjectOutputStream oOut = new ObjectOutputStream(fOut);
oOut.writeObject(s);
oOut.flush();
oOut.close();
fOut.close();

// recharger le tout
FileInputStream fichier = new FileInputStream("serB.ser");
ObjectInputStream is = new ObjectInputStream(fichier);
SerB p = (SerB) is.readObject();
System.out.println(s.x);
System.out.println(s.y);
System.out.println(s.c);


}
}
27 changes: 27 additions & 0 deletions Singleton.java
@@ -0,0 +1,27 @@

public class Singleton {
private static Singleton instance = null;

// la prŽsence de synchronized est obligatoire pour gŽrer les threads!
synchronized public static Singleton getInstance() {
if (instance != null) {
return instance;
}
else {
instance = new Singleton();
return instance;
}
}

private Singleton() {
// fait le travail normal d'un constructeur
System.out.println("1 instance");
}

public static void main(String[] args) {
for (int i=0 ; i<101 ; i++) {
new Singleton();
}
}

}

0 comments on commit b485909

Please sign in to comment.