A thread in java can't be restarted in Java, so I implemented a java Thread , and then tried to restart the thread after getting the serialized object of Thread.
import java.io.Serializable;
public class ThreadSerialization extends Thread implements Serializable {
int iCheck = 10;
@Override
public void run() {
System.out.println("STARTING");
for(int i=0;i<10;i++){
iCheck+=i;
}
}
}
and Serializing algo-
public class CallingThreadSerializable {
public static void main(String[] args) {
ThreadSerialization ser = new ThreadSerialization();
ser.start();
FileOutputStream fos = null;
ObjectOutputStream out = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fos = new FileOutputStream("thread.ser");
out = new ObjectOutputStream(fos);
out.writeObject(ser);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
fis = new FileInputStream("thread.ser");
ois = new ObjectInputStream(fis);
ThreadSerialization ser1 = (ThreadSerialization) ois.readObject();
System.out.println("---> " + ser1.iCheck);
ser1.start();
System.out.println("---> " + ser1.iCheck);
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
OUTPUT-
STARTING
---> 55
---> 55
STARTING
Why the ser1 object is starting again ?
Thread class cannot be serialized due to call to Native Methods!!!
Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as: Sending the object to a remote application by using a web service.
Java thread serialization consists of capturing the current execution state of a Java thread for the purposes of transmission or storage, and thread de-serialization is the complementary process of restoring the execution state of a thread.
Q6) What happens if the object to be serialized includes the references to other serializable objects? Ans) If the object to be serialized includes references to the other objects, then all those object's state also will be saved as the part of the serialized state of the object in question.
There are two points:
First: Thread
is NOT Serializable
and hence the following excerpt from the serializable
JavaDoc applies:
To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields.
Which mean, your class ThreadSerialization
would have the responsibility to store and restore the state of Thread
. But you cannot do that due to many private
fields in Thread
. Therefore all private field in Thread
are default initialized. Now look at implementation of Thread.start()
:
//...
if (threadStatus != 0)
throw new IllegalThreadStateException();
// ...
start0();
//...
Because threadStatus
has not been stored/restored properly you can start a second time.
Second: Don't confuse the actual operating system thread and the "manager" object java.lang.Thread
- they are only loosely coupled. In your example you only serialize the manager but not the OS-thread which has no representation in Java. After deserialization you have a second manager instance which no attached OS-thread. So telling the manager to start will succeed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With