Skip to main content

Posts

Showing posts from May, 2015

What happens if your serializable class contains a member which is not serializable in Java?

It is simple, the class will not be serializable, unless the field is declared transient (which will prevent the field from being serialized, i.e. it will not be saved). It'll throw a NotSerializableException when you try to serialize it. To avoid that, make that field a "transient" field.  private transient YourNonSerializableObject dontSerializeMe; // This won't be serialized

What is "ThreadLocal" in Java?

ThreadLocal class simplifies the development of thread-safe concurrent programs.  It ensures the object stored in this class not shared between threads.  It is as an alternative to synchronization, to improve scalablity in transaction-intensive environments.  Classes encapsulated in ThreadLocal are automatically thread-safe in a pretty simple way, since it's clear that anything stored in ThreadLocal is not shared between threads.