package com.codegym.task.task20.task2015;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/*
Overriding serialization
*/
public class Solution implements Serializable, Runnable {
private transient Thread runner;
private int speed;
public Solution(int speed) {
this.speed = speed;
runner = new Thread(this);
runner.start();
}
public void run() {
// Do something here, doesn't matter what.
System.out.println("The thread has started running");
}
/**
Override serialization.
To do this, you need to declare the following methods:
private void writeObject(ObjectOutputStream out) throws IOException
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
Now serialization/deserialization will work for our scenario :)
*/
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
Thread t1 = new Thread(this);
t1.start();
}
public static void main(String[] args) {
Solution s1 = new Solution(122);
}
}
Andrei
Level 41
What is the point of creating a new thread in the readObject method and starting that thread if that is done already when we create a new object?
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Nouser
26 February 2021, 11:17solution
The problem is, that the Thread is running. You can serialize a Runnable and you can serialize a Thread object. But you can not save a running thread. When deserializing you would need to start the thread again. Here starting is done in the constructor. When deserializing a constructor will not run cause you deserialize already constructed objects.
That's why you got help and runner is transient. And that is easy to do cause here it doesn't contain any relevant data. That is in the runnable. The Thread object just manages the runnable, just has the duty to start the runnable.
So if you want to do some additional work when deserializing you have to do that in the readObject method. When serializing nothing is special and you could omit writeObject. Back to read object. runner is transient and not serialized. So you have to take care of that. You need to create a Thread object and start it. Note again that the Runnable (Solution) is deserialized and the constructor not executed, hence the thread not started.
CG just cecks here (at least I guess) that a Solution Runnable gets started when doing their checks. Don't know if it would work to reassign the new Thread to the runner field. Maybe you want to try and tell me ;) (cause you can not start the same thread twice. But as it is not the same thread, just the same reference variable, it should work.)
+4
Andrei
26 February 2021, 12:17
You mean like this?
The code ran without throwing anything.
Thanks! 0
Nouser
26 February 2021, 13:07
I'd remove that t1 variable entirely...
and thanks for telling me that this works ;)
+1
matemate123
12 February 2023, 12:08
Too little theoretical knowledge for beginners in this task but thanks for back-up Nouser!
0