Java thread serialization, why a serialized Thread object can be run

A thread in java cannot be restarted in Java, so I implemented a Java thread and then tried to restart the thread after receiving a serialized Thread object.

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 Algorithm -

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();
        }

    }

}

CONCLUSION -

STARTING
---> 55
---> 55
STARTING

Why does the ser1 object start again?

+5
source share
5 answers

If you deserialize (?) An Object, then you are essentially creating a new instance of this class with the same properties as the original Object.

This is not the same object.

, Serializable, Serializable ( ), , . , extends Thread implements Serializable, , , , Thread.start() Thread.interrupt() readObject() readResolve(), .

+7

:

: Thread Serializable , , Serializable JavaDoc:

, ,       , ( )    .

, ThreadSerialization Thread. - private Thread. Thread . Thread.start():

    //...
    if (threadStatus != 0)
        throw new IllegalThreadStateException();
    // ...
    start0();
    //...

threadStatus /, .

: "manager" java.lang.Thread - . , OS-, Java. , OS-. , , .

+15

Objects ( .ser), , , , , . , , , .

hashcode , , hashcode JVM , , 2 ... diff, .

+1

, Serializable. , java "Thread", "OutputStream", "Socket", . ? - Thread, System1 JVM, System1, System2 System2 JVM. ! , .

.

ThreadSerialization ser1 = (ThreadSerialization) ois.readObject();// Thread started in System2.
ser1.start();// Thread once again started here in System2.

, Thread Runnable. .

+1

"":)

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

period!

EDIT:

if you work in one process , you can declare a global variable. If you create multiple threads , you can create a class that will contain a list of active threads.

  • This class contains a hash map.
  • The hash map contains the key (id) as String and the value as a weak link to the stream.
  • Themes are identified by id
  • You do not need to worry about GC. If your thread ends or ends, the class method returns null.

import java.lang.ref.WeakReference;
import java.util.HashMap;

public class ThreadsMap {

    private HashMap<String, WeakReference<Thread>> _threadHashMap;

    public ThreadsMap() {
        _threadHashMap = new HashMap<>();
    }

    public void add(String id, Thread thread) {
        WeakReference<Thread> threadWeakReference = new WeakReference<>(thread);
        _threadHashMap.put(id, threadWeakReference);
    }

    public Thread get(String id) {
        WeakReference<Thread> threadWeakReference = _threadHashMap.get(id);
        return threadWeakReference.get();
    }

}
0
source

All Articles