Hello. I'm trying to serialize a class that implements Serializable and a Runnable. My class has a transient Thread object as a field that is created and started after the restoration of my class(object).
This is proven possibile by a tutorial from Oracle, here. But i fail to compile my program which is very similar to the one in the tutorial(i think).
So in the following i attach my source code and the exception's trace in hope that someone can help me out.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ppc2;
import java.io.Serializable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Main {
Main(){
SerializedThread [] threads = new SerializedThread[7];
for(int i =1 ;i<8;i++){
if(i==1)
threads[i] = new SerializedThread(new String[]{"f2","f3","f4"});
if(i==2)
threads[i] = new SerializedThread(new String[]{"f5"});
if(i==3)
threads[i] = new SerializedThread(new String[]{"f6"});
if(i==4)
threads[i] = new SerializedThread(new String[]{"f7"});
}
for(int i =0;i<7;i++){
writeMyObjectToFile(threads[i],"f"+(i+1));
}
readMyObjectFromFile("f1");
}
private void writeMyObjectToFile(SerializedThread runner, String fileName){
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try{
fos = new FileOutputStream(fileName);
oos = new ObjectOutputStream(fos);
oos.writeObject(runner);
oos.close();
}catch(IOException e){
e.printStackTrace();
}
}
private SerializedThread readMyObjectFromFile(String fileName){
FileInputStream fis = null;
ObjectInputStream ois = null;
SerializedThread runner = null;
try{
fis = new FileInputStream(fileName);
ois = new ObjectInputStream(fis);
runner = (SerializedThread)ois.readObject();
ois.close();
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}
return runner;
}
public static void main(String[] args) {
new Main();
}
class SerializedThread implements Serializable, Runnable {
transient private SerializedThread[] children = null;
transient public Thread self = null;
private String[] childrenFileNames = null;
SerializedThread(String[] childrenFileNames) {
setNames(childrenFileNames);
}
public void setNames(String[] childrenFileNames) {
this.childrenFileNames = childrenFileNames;
}
@Override
public void run() {
restoreGivenChildren();//They will auto start, because of the "start" comand in the readObject function.
}
private void restoreGivenChildren() {
if (childrenFileNames != null) {
children = new SerializedThread[childrenFileNames.length];
for (int i = 0; i < childrenFileNames.length; i++) {
children[i] = (SerializedThread) readMyObjectFromFile(childrenFileNames[i]);
//children[i].self=new Thread(this);
children[i].self.start();
}
}
}
// private void writeObject(ObjectOutputStream out) throws IOException {
// out.defaultWriteObject();
// }
//
// private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
// // our "pseudo-constructor"
// in.defaultReadObject();
// // now we are a "live" object again, so let's run rebuild and start
// self = new Thread(this);
// self.start();
// }
}
}
The error>>>
run:
java.io.NotSerializableException: ppc2.Main
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at ppc2.Main.writeMyObjectToFile(Main.java:42)
at ppc2.Main.<init>(Main.java:31)
at ppc2.Main.main(Main.java:67)
java.io.NotSerializableException: ppc2.Main
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at ppc2.Main.writeMyObjectToFile(Main.java:42)
at ppc2.Main.<init>(Main.java:31)
at ppc2.Main.main(Main.java:67)
java.io.NotSerializableException: ppc2.Main
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at ppc2.Main.writeMyObjectToFile(Main.java:42)
at ppc2.Main.<init>(Main.java:31)
at ppc2.Main.main(Main.java:67)
java.io.NotSerializableException: ppc2.Main
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at ppc2.Main.writeMyObjectToFile(Main.java:42)
at ppc2.Main.<init>(Main.java:31)
at ppc2.Main.main(Main.java:67)
BUILD SUCCESSFUL (total time: 0 seconds)