Given is the following code:
import java.io.Serializable;
import java.util.concurrent.locks.ReentrantLock;
class Data
{
private int value;
Data (int value)
{
this.value = value;
}
}
public class InfoCollection implements Serializable
{
private Data[] data;
private static final long serialVersionUID = 1L;
private transient ReentrantLock _lock = new ReentrantLock ();
public InfoCollection (int datasize)
{
this.data = new Data[datasize];
}
public setData (Data newdata, int index)
{
_lock.lock ();
try
{
this.data[index] = newdata;
}
finally
{
_lock.unlock ();
}
}
}
Let’s say I create an object of type InfoCollection and serialize it. After a while I deserialize it and want to use it. After deserialization, in what state will the _lock field be ? Locked or unlocked ? null or not null ? Why ?
This can be easily found out by actually doing it and check the lock’s state, but I want to figure it out logically.
I’m thinking that, after deserialization, the loading of the class/object will trigger a call to the constructor of ReentrantLock (because the constructor is called outside of any method/constructor), which will give you an unlocked object as result. Am I correct ?
In your example lock will be null.
_lock – in transient – it is not stored – so it will be not restored. Constructor will not be called
private transient ReentrantLock _lock = new ReentrantLock ();
(
NOTE
Data is not serializable, you do not have method implemented:
)