Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Thread Serialization , why serialized Thread Object can be started

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 ?

like image 514
Abhishek Choudhary Avatar asked May 16 '12 16:05

Abhishek Choudhary


People also ask

Can threads be serialized Java?

Thread class cannot be serialized due to call to Native Methods!!!

Why do objects need to be serialized?

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.

What is thread serialization in Java?

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.

What happens if the object to be serialized?

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.


1 Answers

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.

like image 56
A.H. Avatar answered Sep 21 '22 04:09

A.H.