Java Interview Questions
Java Interview Questions
------------------------------------------------------------------------
1.Volatile Keyword in Java.
Example of Transient
// A sample class that uses transient keyword to
// skip their serialization.
class TransientExample implements Serializable {
transient int age;
// serialize other fields
private String name;
private String address;
// other code
}
Example of Volatile
class VolatileExmaple extends Thread{
boolean volatile isRunning = true;
public void run() {
long count=0;
while (isRunning) {
count++;
}
System.out.println("Thread terminated." + count);
}
public static void main(String[] args) throws InterruptedException {
VolatileExmaple t = new VolatileExmaple();
t.start();
Thread.sleep(2000);
t.isRunning = false;
t.join();
System.out.println("isRunning set to " + t.isRunning);
}
}