Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Tests sur la serialisation
- Loading branch information
1 parent
e7ce34a
commit b485909
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
} |