Transient-
Transient keyword is used with instance variables to exclude them from serialization process. if a field is transient its value will not be persisted.
when the transient filed is de-serialized it will get the default value
Null in case of reference variable
false in case of boolean
0(zero) in case of other primitive
Ques- when to use transient??
the field whose value can be derived from other fields of the class so mark that object as transient
or the some of the class are not inherently serialize-able because they manage the resource outside the java managed resources like
FileOutputStream or Socket class
or when your class is caching internally - restoration from file is a complex then to recalculate.
is these such scenario we use transient keyword
if logger is used in serialized class make logger as transient
Some Points
-Transient can only be a member variable applying on local variable lead you to some problem
-Transient and final can be used together no compilation error no runtime error at de-serialization., but but it will give you default or null value after de-serialization of final.
-Transient with static no problem in compilation, since static variable are not saved during serialization itself whether transient or not, so not a problem, got the present value.
Volatile- Smart Fix updated by any and will be available to all
Transient keyword is used with instance variables to exclude them from serialization process. if a field is transient its value will not be persisted.
when the transient filed is de-serialized it will get the default value
Null in case of reference variable
false in case of boolean
0(zero) in case of other primitive
Ques- when to use transient??
the field whose value can be derived from other fields of the class so mark that object as transient
or the some of the class are not inherently serialize-able because they manage the resource outside the java managed resources like
FileOutputStream or Socket class
or when your class is caching internally - restoration from file is a complex then to recalculate.
is these such scenario we use transient keyword
if logger is used in serialized class make logger as transient
Some Points
-Transient can only be a member variable applying on local variable lead you to some problem
-Transient and final can be used together no compilation error no runtime error at de-serialization., but but it will give you default or null value after de-serialization of final.
-Transient with static no problem in compilation, since static variable are not saved during serialization itself whether transient or not, so not a problem, got the present value.
Volatile-
Read and write operation on the volatile should be atomic.
read should always be from memory not from the thread cache.
write field always synchronously flushed the memory.
Established the happens before relationship- if any operation happens before its change will be reflected to all
This kind of variable can be used for inter thread communication.
Established the happens before relationship- if any operation happens before its change will be reflected to all
This kind of variable can be used for inter thread communication.
Note-Transient can be used with static but not the good practice, But volatile can be used with static
Ques- when to use volatile??
-Used when visibility part of Synchronization needs to be implemented
i.e Same value will be visible to all thread but not the edit part of synchronization, any thread can edit the volatile variable without any lock
Volatile V/S Synchronization
Simple Example
any thread can edit any can read at any time no synchronization
Synchronization- this is the bad fix
only single thread can edit at a time, but all thread have its own copy of this variable
-Used when visibility part of Synchronization needs to be implemented
i.e Same value will be visible to all thread but not the edit part of synchronization, any thread can edit the volatile variable without any lock
Volatile V/S Synchronization
Simple Example
any thread can edit any can read at any time no synchronization
class SimpleClass {
private volatile int counter;
public void hit(){
counter++;
}
}
Synchronization- this is the bad fix
only single thread can edit at a time, but all thread have its own copy of this variable
class BadExampleFixed {
private int counter;
public synchronized void hit(){
counter++;
}
}
Volatile- Smart Fix updated by any and will be available to all
class GoodExample {
private static volatile int counter;
public static void updateCount(int count){
counter= count; }
public static void main(String[] args) throws Exception{
while(true){
Thread.sleep(2000);
System.out.println("Count current is "+counter);
}
}
}
Either we do modification in volatile variable
or make synchronized method for modification
this will be available to all threads.
Comments
Post a Comment